1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
<canvas id="mycanvas">
</canvas>
<script type="text/javascript">
var canvas = document.getElementById("mycanvas");
var context = canvas.getContext("2d");
var SQUARE_SIZE = 50;
var img = new Image();
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
drawSquare(current_pos);
}
img.src = "snailbait.png";
var stop = false;
var current_pos = {
x: 0,
y: 0,
increment: function() {
if (this.x * SQUARE_SIZE > img.width) {
this.x = 0;
this.y = this.y + 1;
}
else {
this.x = this.x + 1;
}
if (this.y * SQUARE_SIZE > img.height) {
return true;
}
else {
return false;
}
},
getX: function() {
return this.x * SQUARE_SIZE;
},
getY: function() {
return this.y * SQUARE_SIZE;
}
};
var drawSquare = function(pos) {
context.drawImage(
img,
current_pos.getX(),
current_pos.getY(),
SQUARE_SIZE,
SQUARE_SIZE,
current_pos.getX(),
current_pos.getY(),
SQUARE_SIZE,
SQUARE_SIZE);
stop = current_pos.increment();
if (!stop) {
setTimeout(function() {drawSquare(current_pos)}, 1000);
}
} |
Partager