自动滚动与HTML 5画布
Automatic scroll with HTML 5 canvas
我有问题相关的HTML 5画布动画。
我刚刚写了这整个代码,它所做的是一个盒子一直向下移动,超过了网页的高度,由于滚动选项来了,因为我必须向下滚动,看看盒子在哪里。
所以我的问题是:有没有一种方法,当盒子向下移动时,它会自动帮助用户向下滚动,只是通过动画?这意味着当盒子向下移动时,它也会向下滚动,这有助于用户显示盒子的移动位置。如果是背景,那么它就会在那里它只是一个样本。
小提琴在这里:http://jsfiddle.net/gamealchemist/MLHQK/我尝试使用背景滚动,但这也不工作…!
代码是:
css body {
margin: 0px;
padding: 0px;
}
html <canvas id="myCanvas" width="578" height="1000"></canvas>
javascript window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
function drawRectangle(myRectangle, context) {
context.beginPath();
context.rect(myRectangle.x, myRectangle.y, myRectangle.width, myRectangle.height);
context.fillStyle = '#8ED6FF';
context.fill();
context.lineWidth = myRectangle.borderWidth;
context.strokeStyle = 'black';
context.stroke();
}
function animate(myRectangle, canvas, context, startTime) {
// update
var time = Date.now() - startTime;
// pixels / second^2
var gravity = 200;
myRectangle.y = 0.5 * gravity * Math.pow(time / 1000, 2);
if (myRectangle.y > canvas.height - myRectangle.height - myRectangle.borderWidth / 2) {
myRectangle.y = canvas.height - myRectangle.height - myRectangle.borderWidth / 2;
}
lastTime = time;
// clear
context.clearRect(0, 0, canvas.width, canvas.height);
// draw
drawRectangle(myRectangle, context);
// request new frame
requestAnimFrame(function () {
animate(myRectangle, canvas, context, startTime);
});
}
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var myRectangle = {
x: 239,
y: 0,
width: 100,
height: 50,
borderWidth: 5
};
drawRectangle(myRectangle, context);
// wait one second before dropping rectangle
setTimeout(function () {
var startTime = (new Date()).getTime();
animate(myRectangle, canvas, context, startTime);
}, 2000);
将此添加到animate函数中可以解决这个问题
delay = 100;
// scroll
if( myRectangle.y - delay > 0 )
window.scrollTo( 0, myRectangle.y - delay );
这是我从你的代码中做的一个例子,在那里我还添加了一个背景图像,所以你可以看到框正确移动。
<!DOCTYPE html>
<html>
<head>
<script>
window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
function drawRectangle(myRectangle, context) {
context.beginPath();
context.rect(myRectangle.x, myRectangle.y, myRectangle.width, myRectangle.height);
context.fillStyle = '#8ED6FF';
context.fill();
context.lineWidth = myRectangle.borderWidth;
context.strokeStyle = 'black';
context.stroke();
}
function animate(myRectangle, canvas, context, startTime) {
// update
var time = Date.now() - startTime;
// pixels / second^2
var gravity = 200;
myRectangle.y = 0.5 * gravity * Math.pow(time / 1000, 2);
if (myRectangle.y > canvas.height - myRectangle.height - myRectangle.borderWidth / 2) {
myRectangle.y = canvas.height - myRectangle.height - myRectangle.borderWidth / 2;
}
lastTime = time;
// clear
context.clearRect(0, 0, canvas.width, canvas.height);
// draw
drawRectangle(myRectangle, context);
delay = 100;
// scroll
if( myRectangle.y - delay > 0 )
window.scrollTo( 0, myRectangle.y - delay );
// request new frame
requestAnimFrame(function () {
animate(myRectangle, canvas, context, startTime);
});
}
function load(){
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var myRectangle = {
x: 239,
y: 0,
width: 100,
height: 50,
borderWidth: 5
};
drawRectangle(myRectangle, context);
// wait one second before dropping rectangle
setTimeout(function () {
var startTime = (new Date()).getTime();
animate(myRectangle, canvas, context, startTime);
}, 2000);
}
</script>
</head>
<body background="http://upload.wikimedia.org/wikipedia/commons/1/16/Appearance_of_sky_for_weather_forecast,_Dhaka,_Bangladesh.JPG" style="margin: 0px;padding: 0px;" onload="load();">
<canvas id="myCanvas" width="578" height="5000"></canvas>
</body>
</html>
相关文章:
- Html画布自由转换
- HTML画布在绘图后立即擦除矩形
- 在HTML画布上格式化文本
- 如何将要单独填充的每个HTML画布路径/形状分开
- 加载图像后调整HTML画布的大小
- 如何在html画布上替换ByImage()而不是drawImage()(忽略不透明度)
- html画布中的等轴测立方体投影
- 重叠对象上的HTML画布事件
- 使用ja将对象添加到HTML画布中
- Html画布绘制速度因画布元素过多而减慢
- 来自“选择”选项的 HTML 画布功能
- 覆盖 HTML 画布上的“另存为”行为
- HTML 画布像素数据类型
- 在划桨点击时更改 HTML 画布乒乓动画中圆圈的颜色
- HTML 5 画布键控事件动画
- 尝试将无符号长整型的 ARGB 数据绘制到 HTML 画布
- 尝试将一个三.js OBJLoader 放入 HTML 画布中
- HTML画布阴影模糊旋转后不起作用
- 调整大小后HTML画布更新次数过多
- 在html画布中显示一个图像,然后增大其大小