我已经为 html5 画布中的动画实现了平移功能。这可能是我制作的东西最令人作呕的实现,或者看起来哈哈,但它有效,现在已经足够好了。
所以我设置了这些变量——为了简单起见,我将排除 Y 轴逻辑以及任何保存和恢复。
var translationX = 0; //This is where to start the clipping region
这是我的绘画逻辑。Canvas 是要绘制的图像,bCanvas 是放置在图像上的“视口”。当我没有放大时,它只会显示整个画布图像。所有作品都按此处的预期工作。
function drawBuffer(){
//If zoomed in I set the clipping region dimensions to half,
//having the effect of scaling by 2.
if(stateManager.getZoomState() == "zoomIn")
bContext.drawImage(canvas,translationX,translationY,
bCanvas.width*0.5,bCanvas.height*0.5,
0,0,
bCanvas.width,bCanvas.height);
//Otherwise just draw the image to a scale of 1
else
bContext.drawImage(canvas,0,0,bCanvas.width,bCanvas.height);
}
这是沿 X 轴在画布上移动剪切区域的逻辑,问题出在哪里 - 条件是说明何时停止递增 translationX。
function moveClippingRegion(){
if(state is set to zoomed in){
if(Right arrow key is pressed){
//This condition admittedly is just the result of hacking away, but it works
// in the sense that I stop incrementing translationX when
//at the edge of the canvas
if(translationX>= (bCanvas.width -canvas.width) - translationX - 64){
//The -64 literal is definitely a hacking attempt,
//but without the clipping region increments two times more than
//it should, thus going off the canvas.
}
else
translationX += 16;
}
}
好的,所以当窗口没有调整大小时,这一切都有效,所以我正在寻找的是一些方向,即在调整 bCanvas 大小的情况下放入什么条件。
这是调整大小的功能,以防万一很重要:
function onResize(){
bCanvas.width = window.innerWidth * 0.60952380952380952380952380952381;
bCanvas.height = window.innerHeight * 0.83116883116883116883116883116883;
drawBuffer();
}
绘图画布的尺寸是 1024 x 768,所以那些丑陋的文字只是使 bCanvas 的尺寸与画布相同。
这仅适用于我的屏幕分辨率,并且当窗口尚未调整大小时。我竭尽全力试图让它独立于分辨率/调整窗口大小后工作。
最后,这里有一张图像来帮助说明在不同分辨率下发生的问题/在调整窗口大小后,如果窗口已调整为非常小的大小,则剪辑区域甚至不会移动,这在 logc 中是可以预料的moveClippingRegion 函数中的 if 语句:
感谢您提前阅读本文,任何帮助将不胜感激!