【发布时间】:2016-04-30 07:32:33
【问题描述】:
我正在尝试模仿这个网站的流畅程度:http://material.cmiscm.com/(你可能以前见过)
所以,我从非常小的开始,只是试图模拟紫色部分的渐变从盒子中消失的方式。他的渐变是对角的——为简单起见,我的渐变是水平的。
但是,我的问题是我的渐变非常不稳定,即使我尝试更改RECT_INCREMENT的值
小提琴: https://jsfiddle.net/16he6jfu/
原始代码(ctrl +v'able):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<style>
#canvas {
padding: 0;
margin: 0;
}
body {
background: #6D00D0;
}
</style>
<body>
<canvas id="canvas" width="900" height="555">
</canvas>
</body>
<script>
var MAX_RECT_LENGTH = 800;
var RECT_INCREMENT = 50;
var RECT_X_ORIG = 0;
var RECT_Y_ORIG = 100;
var RECT_Y_MIN = 0;
var RECT_Y_MAX = 100;
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var gra = context.createLinearGradient(0, 0, 800, 0);
gra.addColorStop(0, 'rgba(229, 88, 95, .6)');
gra.addColorStop(1, 'rgba(255, 255, 255, 0)');
var path = 0;
function draw(){
if (path < MAX_RECT_LENGTH) {
context.beginPath();
// 1
context.moveTo(RECT_X_ORIG + path, RECT_Y_ORIG);
//console.log('1) ' + (RECT_X_ORIG + path) + ', ' + RECT_Y_ORIG);
// 2
path = path+RECT_INCREMENT;
context.lineTo(RECT_X_ORIG + path, RECT_Y_ORIG);
//console.log('2) ' + (RECT_X_ORIG + path) + ', ' + RECT_Y_ORIG);
// 3
context.lineTo(RECT_X_ORIG + path, RECT_Y_MIN);
//console.log('3) ' + (RECT_X_ORIG + path) + ', ' + RECT_Y_MIN);
// 4
context.lineTo(RECT_X_ORIG + path - RECT_INCREMENT, RECT_Y_MIN);
//console.log('4) ' + (RECT_X_ORIG + path - RECT_INCREMENT) + ', ' + RECT_Y_MIN);
// 5
context.lineTo(RECT_X_ORIG + path - RECT_INCREMENT, RECT_Y_MAX);
//console.log('5) ' + (RECT_X_ORIG + path - RECT_INCREMENT) + ', ' + RECT_Y_MAX);
context.closePath();
context.fillStyle=gra;
context.fill();
}
window.requestAnimationFrame(draw);
}
window.onload = function() {
window.requestAnimationFrame(draw);
}
</script>
</html>
【问题讨论】:
-
在我的机器上看起来不错。
-
我的也是。也许你的机器被征税或废话。
-
疯了!我的机器很垃圾,但不会落后于我链接的网站,但会落后于我的网站。这对我来说可能很难解决。谢谢大家!
-
@HC_ 听起来对我来说是正确的 - 将位图复制到位图而不是光栅化通常更快,您也可以从中做一部分。
标签: javascript html css canvas css-animations