我想在HTML5畫布中繪製甜甜圈。如果畫布的背景顏色是純色,我可以繪製它。但它是漸變色,我無法繪製它。如何使用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();
}
- 如果畫布的背景顏色是純色:
var context = canvas.getContext("2d")
, properties = {
"background-fill-type": "solid", //solid color
"background-color": "#FFFFFF",
"background-first-color": "#008B8B",
"background-second-color": "#F5DEB3",
"background-gradient-factor": 0.5,
"border-color": "#FFFFFF",
"border-thickness": 0
};
//draw canvas background (solid color)
background(context, {
x: 0,
y: 0,
w: properties["width"],
h: properties["height"]
}, properties);
//draw doughnut
context.save();
context.beginPath();
context.translate(centerX, centerY);
context.arc(0, 0, Radius, 0, dpi, false); //外部圓
context.fillStyle = "blue";
context.fill();
context.closePath();
context.beginPath();
context.arc(0, 0, radius, 0, dpi, false); //內部圓
context.fillStyle = properties["background-color"];
context.fill();
context.closePath();
context.restore();
- 如果畫布的背景顏色是漸變色:
var context = canvas.getContext("2d")
, properties = {
"background-fill-type": "gradient", //gradient color
"background-color": "#FFFFFF",
"background-first-color": "#008B8B",
"background-second-color": "#F5DEB3",
"background-gradient-factor": 0.5,
"border-color": "#FFFFFF",
"border-thickness": 0
};
//draw canvas background (gradient color)
background(context, {
x: 0,
y: 0,
w: properties["width"],
h: properties["height"]
}, properties);
//draw doughnut
context.save();
context.beginPath();
context.translate(centerX, centerY);
context.arc(0, 0, Radius, 0, dpi, false); //外部圓
context.fillStyle = "blue";
context.fill();
context.closePath();
context.beginPath();
context.arc(0, 0, radius, 0, dpi, false); //內部圓
//context.fillStyle = properties["background-color"];
// *----------------------------------------------------------------------*
// | How to solve internal circle and canvas background color consistent? |
// |
// *----------------------------------------------------------------------*
context.fill();
context.closePath();
context.restore();
這是一種效果圖(A小歪, - - ! ):
究竟你的意思是你「不能得出它」?當你嘗試時會發生什麼?另外,請張貼繪圖的代碼。 – Philipp 2013-04-09 11:47:02
我忘了把代碼。對不起。 – Kinematic 2013-04-10 01:32:15