[英]Drawing shapes and lines with HTML5 Canvas and jQuery
I have an upcoming project that I would like to use the HTML5 canvas element to accomplish what would have had to be done in the past with either images and absolutely paced div's or Flash. 我有一个即将推出的项目,我想使用HTML5 canvas元素来完成过去必须要做的事情,无论是图像还是绝对节奏的div或Flash。 Here is a basic example of the concept
这是该概念的基本示例
Here are my concerns: 以下是我的担忧:
Can anyone shed some light on this and offer any suggestions? 任何人都可以对此有所了解并提供任何建议吗? Is there an advantage to using any of the jQuery canvas plugiins over vanilla JS?
使用任何jQuery canvas插件而不是vanilla JS是否有优势?
thank you! 谢谢!
The canvas API consists of some functions which seem to do the job just fine: canvas API包含一些似乎可以正常工作的函数:
.moveTo
/ .lineTo
for a line path .moveTo
/ .lineTo
表示行路径 .arc
for a circle path .arc
为圆形路径 .stroke
to stroke a path (line) .stroke
描边路径(线) .fill
to fill a path (circle) .fill
填充路径(圆圈) Here's a very trivial proof of concept: http://jsfiddle.net/eGjak/275/ . 这是一个非常简单的概念证明: http : //jsfiddle.net/eGjak/275/ 。
I've used (x, y)
for both the lines and the circles, which means the lines go from and to the midpoint of two circles. 我已经将
(x, y)
用于线条和圆圈,这意味着线条从两个圆圈的中点开始。 r
is the radius of a circle. r
是圆的半径。
var ctx = $('#cv').get(0).getContext('2d');
var circles = [ // smaller circles
{ x: 50, y: 50, r: 25 },
{ x: 250, y: 50, r: 25 },
{ x: 250, y: 250, r: 25 },
{ x: 50, y: 250, r: 25 },
];
var mainCircle = { x: 150, y: 150, r: 50 }; // big circle
function drawCircle(data) {
ctx.beginPath();
ctx.arc(data.x, data.y, data.r, 0, Math.PI * 2); // 0 - 2pi is a full circle
ctx.fill();
}
function drawLine(from, to) {
ctx.beginPath();
ctx.moveTo(from.x, from.y);
ctx.lineTo(to.x, to.y);
ctx.stroke();
}
drawCircle(mainCircle); // draw big circle
$.each(circles, function() { // draw small circles and lines to them
drawCircle(this);
drawLine(mainCircle, this);
});
You could just do all of these circles in CSS. 你可以在CSS中完成所有这些圈子。 Get some divs, style them as you like in CSS, and then apply border-radius: 100;
获取一些div,在CSS中根据需要设置样式,然后应用border-radius:100; to the object, and done.
到了对象,并完成了。 I hope this helped.
我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.