我想在HTML5画布中绘制甜甜圈.如果画布的背景颜色是纯色,我就可以绘制它.但它是渐变色,我无法画出来.
当画布的背景颜色是渐变色时,我想知道如何绘制甜甜圈.
喜欢:

这是我的代码:
function background(context, coordinate, properties) {
var x = coordinate.x //???x
, y = coordinate.y //??? y
, w = coordinate.w //??(??-????????)
, h = coordinate.h //??(??-????????)
, gradientFactor, gradientColor; //????, ???
context.save();
switch( properties["background-fill-type"] ) {
case "solid":
context.fillStyle = properties["background-color"];
break;
case "gradient":
gradientFactor = properties["background-gradient-factor"];
gradientColor = context.createLinearGradient(x, y, x + w, y);
gradientColor.addColorStop(gradientFactor, properties["background-first-color"]);
gradientColor.addColorStop(1 - gradientFactor, properties["background-second-color"]);
context.fillStyle = gradientColor;
break;
case "image":
break;
}
context.fillRect(x, y, w, h);
context.restore();
} …Run Code Online (Sandbox Code Playgroud) 我想用HTML5 <canvas>元素编写一个蛇游戏演示.我试图注册键盘(onkeydown)事件,但它不起作用.
码:
canvas.onkeydown = divertDirection; //don't work
//canvas.addEventListener("keydown", divertDirection, false); //don't work
//?????????????
function divertDirection(ev) {
console.log(ev);
}
//register this event for widnow
window.addEventListener("keydown", divertDirection, false); //ok
Run Code Online (Sandbox Code Playgroud)