假设我有一个精灵地图,我怎样才能用画布绘制它的一部分?
例如,我有一个500x500的图像,但我想只绘制从0到50的部分,我怎样才能在JS中实现它?
这是我的HTML和Javascript代码atm -
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>bla bla</title>
<script>
window.onload = function() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var image = new Image();
image.src = "gimatrix_letters.png";
image.onload = function() {
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 0, 0);
}
}
</script>
</head>
<body>
<canvas id="canvas">
</canvas>
</body>
</html>
TIA