我有一個畫布元素,當加載時會自動填充客戶端的整個瀏覽器窗口。在它上面,你可以用鼠標進行繪製,就像任何「製作繪圖板」 - 在那裏的教程一樣。然而,我想要做的是,如果你將鼠標移動到畫布的任何極端(或者選擇某個「移動」工具,你可以將畫布向任意方向拖動),那麼滾動。特別是,我希望它有可能在理論上永遠滾動,所以我不能真正預生成,我必須在飛行中生成「更多畫布」。有沒有人知道如何做到這一點?如何無限期地製作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