Canvas Flower experiment
Working with Canvas, setTimeout and some math to see if I can create an enclosed shape with some wind based rules about staying inside
by sperske
HTML
<canvas id='world' height='600' width='600'/>
<!-- [Controls with the arrow keys, be sure the 'Results' window has focus']
[UP] add velocity up to `maxVelocity`
[DOWN] subtract velocity down to 0
[LEFT] turn left
[RIGHT] turn right
-->
CSS
#world {
width: 600px;
height: 600px;
}
JavaScript
var maxVelocity = 10,
agility = 5,
baseLength = 5,
degree = ((2*Math.PI)/360),
world = document.getElementById('world'),
context = world.getContext("2d"),
boundry = [[180, 120],[240, 60],[360, 40],[420, 120],[360, 220],[350, 240],[360, 265],[470,360],[450,480],[360,540],[240,550],[140,480],[120,470],[100,360],[120,300],[220,240],[240,220]],
camera = {
location: {
x:300,
y:90
},
angle: 0,
velocity: 0
},
engine = {
drawWorld: function(shape, context) {
var point,
index,
size = shape.length;
context.clearRect(0, 0, world.width, world.height);
context.beginPath();
for(index = 0; index < size; index++) {
point = shape[index];
if(index == 0) {
context.moveTo(point[0], point[1]);
} else {
context.lineTo(point[0], point[1]);
}
}
context.closePath();
context.stroke();
},
drawCamera: function(camera, context) {
var a = camera.location,
b = this.calcNextPoint(camera, 1);
context.beginPath();
context.moveTo(a.x, a.y);
context.lineTo(b.x, b.y);
context.stroke();
context.beginPath();
context.arc(a.x, a.y, baseLength, 0, Math.PI*2, true);
context.closePath();
context.stroke();
},
calcNextPoint: function(camera, moment) {
return {
x: camera.location.x + ((camera.velocity*(1/moment))*Math.sin(camera.angle)),
y: camera.location.y + ((camera.velocity*(1/moment))*(Math.cos(camera.angle)))
};
}
};
engine.drawWorld(boundry, context);
engine.drawCamera(camera, context);
document.onkeydown = function(e) {
e = e ||...