Last active
April 19, 2016 00:08
-
-
Save kuon/6c5af5362f911fc54d3c3b94c7713e5f to your computer and use it in GitHub Desktop.
Basic logic demo of drawing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ctx = canvas.getContext('2d') | |
const draw = () => { | |
drawObjects.forEach((obj: DrawObject) => { | |
obj.draw(ctx) | |
}) | |
requestAnimationFrame(draw) | |
} | |
requestAnimationFrame(draw) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DrawObject { | |
draw(ctx: CanvasRenderingContext2D) { | |
if (this.points.length < 2) { | |
return; | |
} | |
ctx.beginPath(); | |
ctx.moveTo(this.points[0][0], this.points[0][1]); | |
this.points.forEach((point: number[], idx: number) { | |
if (idx === 0) { | |
return | |
} | |
ctx.lineTo(point[0], point[1]) | |
}) | |
ctx.stroke() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment