| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<title>Shoot a cannon</title> |
| •5 |
<link rel="stylesheet" type="text/css" href="/jsxgraph/distrib/jsxgraph.css" /> |
| •6 |
<script type="text/javascript" src="/javascript/jquery/jquery.min.js"></script> |
| •7 |
<script type="text/javascript" src="/jsxgraph/distrib/jsxgraphcore.js"></script> |
| 8 |
</head> |
| 9 |
<body> |
| 10 |
<div id="cannon" class="_jxgbox" style="width: 800px; height: 400px;"></div> |
| 11 |
|
| 12 |
<script type="text/javascript"> |
| 13 |
/* <![CDATA[ */ |
| 14 |
|
| 15 |
(function () { |
| 16 |
var board = JXG.JSXGraph.initBoard('cannon', {boundingbox: [0, 10, 20, 0], showCopyright: false, showNavigation: false, minimizeReflow: 'svg'}), |
| 17 |
bottom = board.create('image', ['img/cannon_bottom.png', [0, 0.3], [2, 1]], {layer: 1}), |
| 18 |
muzzle = board.create('image', ['img/cannon_muzzle.png', [0, 1.05], [3, 0.75]], {layer: 0}), |
| 19 |
shoot = board.create('text', [9, 9.5, '<button id="shoot">Shoot</button>']), |
| 20 |
reset = board.create('text', [11, 9.5, '<button id="reset">Reset</button>']), |
| 21 |
rot, rp, cp, cc, cannonball, cbanim, solution, velocity, |
| 22 |
|
| 23 |
animTime = function () { |
| 24 |
return 3000; |
| 25 |
}, |
| 26 |
|
| 27 |
fAngle = function () { |
| 28 |
return Math.atan2(cp.Y()-rp.Y(),cp.X()-rp.X()); |
| 29 |
}, |
| 30 |
|
| 31 |
fSolution = function (x) { |
| 32 |
var b = fAngle(), |
| 33 |
g = 9.81/2; |
| 34 |
|
| 35 |
return Math.tan(b)*(x - cannonball.X()) - g*(Math.pow((x - cannonball.X())/(velocity.Value()*Math.cos(b)), 2)) + cannonball.Y(); |
| 36 |
}, |
| 37 |
|
| 38 |
fAnim = function (t) { |
| 39 |
var i = Math.floor(solution.points.length*t/animTime())/*3 + 17*t/animTime()*/, |
| 40 |
s = NaN; |
| 41 |
|
| 42 |
if (JXG.exists(solution.points[i]) && solution.points[i].usrCoords[2] > 0) { |
| 43 |
s = solution.points[i].usrCoords.slice(1); |
| 44 |
} |
| 45 |
|
| 46 |
return s; |
| 47 |
}; |
| 48 |
|
| 49 |
board.options.animationDelay = 50; |
| 50 |
|
| 51 |
velocity = board.create('slider', [[1, 9.5], [6, 9.5], [0, 5, 15]], {name: 'Velocity'}); |
| 52 |
|
| 53 |
cp = board.create('point', [1.4, 1.4], {fixed: true, visible: false}); |
| 54 |
cc = board.create('circle', [cp, 1.25], {visible: false}); |
| 55 |
rp = board.create('glider', [0, 1.4, cc], {withLabel: false, showInfobox: false, color: 'black'}); |
| 56 |
|
| 57 |
rot = board.create('transform', [fAngle, cp], {type: 'rotate'}); |
| 58 |
rot.bindTo(muzzle); |
| 59 |
|
| 60 |
cannonball = board.create('point', [3, 1.4], {size: 8, strokeColor: 'black', fillColor: 'gray', withLabel: false, fixed: true}); |
| 61 |
cbanim = board.create('point', [3, 1.4], {size: 8, strokeColor: 'black', fillColor: 'gray', withLabel: false, fixed: true, visible: false}); |
| 62 |
rot.bindTo(cannonball); |
| 63 |
solution = board.create('plot', [fSolution, function () { return cannonball.X(); }, 20], {visible: true, doAdvancedPlot: false}); |
| 64 |
|
| 65 |
|
| 66 |
$('#shoot').on('click', function () { |
| 67 |
cannonball.setProperty({ |
| 68 |
visible: false |
| 69 |
}); |
| 70 |
cbanim.setProperty({ |
| 71 |
visible: true |
| 72 |
}); |
| 73 |
cbanim.moveTo([cannonball.X(), cannonball.Y()]); |
| 74 |
|
| 75 |
cbanim.moveAlong(fAnim, animTime(), { |
| 76 |
callback: function () { |
| 77 |
// this is executed when the animation is finished |
| 78 |
} |
| 79 |
}); |
| 80 |
}); |
| 81 |
|
| 82 |
$('#reset').on('click', function () { |
| 83 |
board.stopAllAnimation(); |
| 84 |
//rp.moveTo([0, 1.4]); |
| 85 |
//cannonball.moveTo([3, 1.4]); |
| 86 |
cbanim.setProperty({visible: false}); |
| 87 |
cannonball.setProperty({visible: true}); |
| 88 |
}); |
| 89 |
|
| 90 |
})(); |
| 91 |
|
| 92 |
/* ]]> */ |
| 93 |
</script> |
| 94 |
<br /><h3>License</h3> |
| 95 |
This example was created with JSXGraph and is licensed CC-BY-SA. Please attribute "Michael Gerhaeuser, jsxgraph.org".<br /> |
| 96 |
The picture of the cannon is licensed CC-BY-SA. Original image is from John Wetzel, an author at wikipremed.com. |
| 97 |
</body> |
| 98 |
</html> |