如何改变html5 canvas的起源?

时间:2012-01-08 22:16:16

标签: html5 canvas

我找不到html5画布的答案。我可以更改原点的坐标,这样我的grafics将完全向右移动15px(仅作为示例)。鉴于以下html和css?

<canvas id="g" width="auto" height="auto">Your browser doesnt support canvas tag</canvas>

的CSS:

canvas, section {
    display: block;
}    

#g {
    position: absolute;
    width:100%;
    height:100%;
    overflow:hidden; 
}

1 个答案:

答案 0 :(得分:11)

听起来你需要translate transform。根据您在上下文中执行的其他操作,您可能还需要调用save() and restore()

var ctx = document.getElementById('myCanvas').getContext('2d');
ctx.save();
ctx.translate(15, 0);
// ... do stuff with the transformed origin
ctx.restore();
// ... do stuff with the canvas restored to its original state (if necessary)