Computer Science 15-237
Class Notes: Getting Started with HTML5 Canvas
<html>
<canvas id="myCanvas" width="400" height="400"
style="border:1px solid #000000;">
No canvas available!
</canvas>
<script src="test.js">
</script>
</html>var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.font = "60px Arial";
ctx.textAlign = "center";
ctx.fillText("Hello World!", 200, 200);var img = new Image(); img.src = "sampleImage.jpg"; img.onload = function(){ var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0); // draw image at (0, 0) };
ctx.drawImage(img, 0, 0, 150, 150); // resize to 150x150
//drawing a slice of the image at a specific coordinate in a specific size //slice: taken from (sx, sy) relative to the top left of the image //slice: of width sWidth and height sHeight //slice: drawn at (dx, dy) on the canvas //slice: drawn with width dWidth and height dHeight on the canvas //ctx.drawImage(img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight); ctx.drawImage(img, 50, 50, 250, 250, 200, 200, 100, 100);
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem