简体   繁体   English

使用HTML5 Canvas和jQuery绘制形状和线条

[英]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: 以下是我的担忧:

  1. I am ok with using div's with corner radius to create the circles as they will be styled, and I'm not sure if I can do that with a mix of svg and the canvas element. 我可以使用带有圆角半径的div来创建圆圈,因为它们将被设计样式,我不确定我是否可以使用svg和canvas元素的混合来做到这一点。
  2. My main concern is the stroke that joins the outer circles to the inner, I would like to do this with canvas but am not sure A) How to get multiple canvas elements on one page in one containing element (a wrapper div) and B) how to figure out the starting points, I would assume the ending point would just be the center of the wrapper div (IE if its 600x600 = x=300, y=300) 我主要担心的是将外圈连接到内部的笔划,我想用canvas进行此操作但不确定A)如何在一个包含元素(包装div)和B)的一个页面上获取多个canvas元素如何弄清楚起点,我会假设终点只是包装div的中心(IE如果600x600 = x = 300,y = 300)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM