简体   繁体   English

在HTML5 Canvas中对形状和文本进行分组

[英]Grouping Shapes and Text in HTML5 Canvas

So I have just started working with HTML5 and the Canvas element. 所以我刚开始使用HTML5和Canvas元素。 I'm working on a project where I will be creating mind maps, and I plan to do this using the Canvas element alongside java script. 我正在开发一个我将创建思维导图的项目,我计划使用Canvas元素和java脚本一起完成。

My questions is, How do I group shapes together in a canvas? 我的问题是,如何在画布中将形状组合在一起? I have no issues drawing shapes and text onto the canvas, and no issues dragging them around the canvas. 我没有问题在画布上绘制形状和文本,并且没有问题在画布上拖动它们。 What I would like to do is lock say a shape and text element together so that if I drag the shape, the text comes along with it. 我想要做的是锁定一个形状和文本元素,以便如果我拖动形状,文本随之而来。

Any ideas? 有任何想法吗?

Thanks in advance. 提前致谢。

You probably want to use SVG instead of canvas. 您可能想要使用SVG而不是画布。

  • SVG is a board upon which you stick paths, groups, etc. which you can add, modify, etc. as they remain separate entities. SVG是一个可以粘贴路径,组等的板,您可以添加,修改等等,因为它们是独立的实体。 You can then have things like an onclick handler on an object (path, group or whatever), which makes event handling often a lot simpler than canvas. 然后,您可以在对象(路径,组或其他)上使用onclick处理程序,这使得事件处理通常比canvas更简单。 Making repeatable content is easier with SVG, too. 使用SVG也可以轻松制作可重复的内容。 Draw something in Inkscape and then you can just copy the SVG it produces and do whatever with it. 在Inkscape中绘制一些东西然后你可以复制它生成的SVG并用它做任何事情。

  • Canvas is just a canvas to paint on; 画布只是一幅画画; each new frame you draw on top of the previous, manually - so instead of having an <ellipse> you have to call functions on your canvas's 2D context to construct every frame. 您手动绘制在上一个上面的每个新框架 - 因此,不必使用<ellipse>而是必须在画布的2D上下文中调用函数来构造每个框架。 No event handlers for that ellipse - you'll need to calculate if the mouse is over the ellipse manually: there is only a single element; 没有该椭圆的事件处理程序 - 您需要计算鼠标是否手动在椭圆上:只有一个元素; the strokes of the paint brush on the canvas are not kept separate. 画布上画笔的笔划不是分开的。 For mind maps that would make it more difficult. 因为思维导图会使其变得更加困难。

SVG also makes export easy and scalable which might be desirable for your purpose. SVG还使出口变得容易和可扩展,这可能是您的目的所希望的。 Canvas would only give you a bitmap. Canvas只会给你一个位图。

I am a little late to the party but fwiw there is a very nice library that let's you do layers , shapes, groups and even event bindings on HTML5's canvas element called Kinetic.js 我有点晚了,但是有一个非常好的库让你在HTML5的canvas元素Kinetic.js上做图层,形状,组甚至事件绑定

https://github.com/ericdrowell/KineticJS/ https://github.com/ericdrowell/KineticJS/

I used this to make a draggable group of shapes and text on html5 canvas with great success. 我用这个在html5画布上制作了一组可拖动的形状和文字,取得了巨大的成功。

Whatever mechanism you are using to drag your shape should do the magic for you if you can temporarily combine those two shapes into one. 无论你使用什么机制拖动你的形状,如果你可以将这两个形状暂时合并为一个,那么你应该为你做出魔力。 Without knowing how you represent shapes and the method you use to move them, I cannot give you any better advice. 在不知道你如何表示形状和你用来移动它们的方法的情况下,我无法给你任何更好的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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