使用画布仅从Image中绘制一部分

时间:2014-12-05 15:26:47

标签: javascript html5 canvas html5-canvas

假设我有一个精灵地图,我怎样才能用画布绘制它的一部分?

例如,我有一个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

0 个答案:

没有答案