我有一个画布元素,当加载时会自动填充客户端的整个浏览器窗口。在它上面,你可以用鼠标进行绘制,就像任何“制作绘图板” - 在那里的教程一样。然而,我想要做的是,如果你将鼠标移动到画布的任何极端(或者选择某个“移动”工具,你可以将画布向任意方向拖动),那么滚动。特别是,我希望它有可能在理论上永远滚动,所以我不能真正预生成,我必须在飞行中生成“更多画布”。有没有人知道如何做到这一点?如何无限期地制作html画布“滚动”?
如果有帮助,这是客户端,现在的javascript:(html的只是一个画布标记)
$(document).ready(function() {
init();
});
function init() {
var canvas = document.getElementById('canvas')
, ctx = canvas.getContext('2d')
, width = window.innerWidth
, height = window.innerHeight;
// Sets the canvas size to be the same as the browser size
canvas.width = width;
canvas.height = height;
// Binds mouse and touch events to functions
$(canvas).bind({
'mousedown': startDraw,
'mousemove': draw,
'mouseup': stopDraw,
});
};
// Triggered on mousedown, sets draw to true and updates X, Y values.
function startDraw(e) {
this.draw = true;
this.X = e.pageX;
this.Y = e.pageY;
};
// Triggered on mousemove, strokes a line between this.X/Y and e.pageX/Y
function draw(e) {
if(this.draw) {
with(ctx) {
beginPath();
lineWidth = 4;
lineCap = 'round';
moveTo(this.X, this.Y);
lineTo(e.pageX, e.pageY);
stroke();
}
this.X = e.pageX;
this.Y = e.pageY;
}
};
// Triggered on mouseup, sets draw to false
function stopDraw() {
this.draw = false;
};
您是否尝试设置一个变量来保存X和Y滚动值,然后将其添加到您绘制的任何坐标中? – Delta 2011-03-29 04:09:50
你有没有得到这个工作版本? – skalb 2012-03-21 20:51:32
[Make a canvas infinite]可能的重复(http://stackoverflow.com/questions/38589304/make-a-canvas-infinite) – bummi 2016-12-31 16:38:46