SVG 和 HTML5 Canvas 有什么区别?
SVG 和 HTML5 Canvas 有什么区别?他们似乎都对我做了同样的事情。基本上,它们都使用坐标点绘制矢量图稿。
我缺少什么? SVG 和 HTML5 Canvas 之间的主要区别是什么?我为什么要选择其中之一而不是另一个?
What are the differences between SVG and HTML5 Canvas? They both seem to do the same to me. Basically, they both draw vector artwork using coordinate points.
What am I missing? What are the major differences between SVG and HTML5 Canvas? Why should I choose one over the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
SVG 就像一个“绘图”程序。绘图被指定为每个形状的绘图指令,并且任何形状的任何部分都可以改变。绘图是面向形状的。
Canvas 就像一个“绘画”程序。一旦像素到达屏幕,这就是您的绘图。除非用其他像素覆盖形状,否则无法更改形状。绘画是面向像素的。
能够更改绘图对于某些程序来说非常重要;例如绘图应用程序、图表工具等。因此 SVG 在这方面具有优势。
对于某些艺术程序来说,能够控制单个像素非常重要。
使用 Canvas 比 SVG 更容易通过鼠标拖动为用户操作提供出色的动画性能。
计算机屏幕上的单个像素通常会消耗 4 个字节的信息,而如今的计算机屏幕需要几兆字节。因此,如果您想让用户编辑图像然后再次上传,Canvas 可能会很不方便。
相比之下,使用 SVG 绘制覆盖整个屏幕的少量形状会占用几个字节,下载速度很快,并且可以轻松地再次上传,在该方向上与在另一个方向上时具有相同的优势。所以SVG可以比Canvas更快。
Google 使用 SVG 实现了 Google 地图。这使得网络应用程序具有快速的性能和平滑的滚动。
SVG is like a "draw" program. The drawing is specified as drawing instructions for each shape and any part of any shape can be changed. Drawings are shape-oriented.
Canvas is like a "paint" program. Once the pixels hit the screen, that is your drawing. You cannot change shapes except by overwriting them with other pixels. Paintings are pixel-oriented.
Being able to change drawings is very important for some programs; e.g. drafting apps, diagramming tools, etc. So SVG has an advantage here.
Being able to control individual pixels is important for some artistic programs.
Getting great animation performance for user-manipulation via mouse drags is easier with Canvas than SVG.
A single pixel on the computer screen will often consume 4 bytes of information and a computer screen these days takes several megabytes. So Canvas might be inconvenient if you want to let the user edit an image and then upload it again.
By contrast, drawing a handful of shapes that cover the entire screen using SVG takes up few bytes, downloads quickly, and can be uploaded again easily with the same advantages going in that direction as when it comes down on the other direction. So SVG can be faster than Canvas.
Google implemented Google Maps with SVG. That gives the web app its zippy performance and smooth scrolling.
请参阅维基百科:http://en.wikipedia.org/wiki/Canvas_element
更新:
我使用 SVG 是因为它的标记语言功能 - 它可以由 XSLT 处理,并且可以在其节点中保存其他标记。同样,我可以在我的标记(化学)中保存 SVG。这允许我通过标记组合来操作 SVG 属性(例如渲染)。这在 Canvas 中可能是可能的,但我怀疑这要困难得多。
See Wikipedia: http://en.wikipedia.org/wiki/Canvas_element
UPDATE:
I use SVG because of its markup language abilities - it can be processed by XSLT and can hold other markup in its nodes. Similarly I can hold SVG in my markup (chemistry). This allows me to manipulate SVG attributes (e.g. rendering) by combinations of markup. This may be possible in Canvas but I suspect that it's a lot harder.
Canvas 与 SVG 的高级摘要
Canvas
SVG
有关详细差异,请阅读 http://msdn.microsoft.com/en-us/library/ie/gg193983(v=vs.85) .aspx
High Level Summary of Canvas vs. SVG
Canvas
SVG
For detailed difference, read http://msdn.microsoft.com/en-us/library/ie/gg193983(v=vs.85).aspx
它们的本质以及为您做的事情是有区别的。
详细说明一下格式与 API:
使用 svg,您可以在许多不同的工具中查看、保存和编辑文件。使用画布,您只需进行绘制,除了屏幕上生成的图像之外,不会保留您刚刚所做的任何内容。您可以对两者进行动画处理,SVG 只需查看指定的元素和属性即可为您处理重绘,而使用 canvas 时,您必须使用 API 自己重绘每个帧。您可以缩放两者,但 SVG 会自动执行此操作,而再次使用画布时,您必须重新发出给定尺寸的绘图命令。
There's a difference in what they are, and what they do for you.
To elaborate a bit, on format versus API:
With svg you can view, save and edit the file in many different tools. With canvas you just draw, and nothing is retained about what you just did apart from the resulting image on the screen. You can animate both, SVG handles the redrawing for you by just looking at the elements and attributes specified, while with canvas you have to redraw each frame yourself using the API. You can scale both, but SVG does it automatically, while with canvas again, you have to re-issue the drawing commands for the given size.
SVG 和 Canvas 对我影响最大的两件事是,
能够在没有 DOM 的情况下使用 Canvas,因为 SVG 严重依赖 DOM,并且随着复杂性的增加,性能会下降。就像游戏设计一样。
使用 SVG 的优点是跨平台分辨率保持不变,这是 Canvas 所缺乏的。
该网站提供了更多详细信息。 http://dev.opera.com/articles/view/svg -或画布在两者之间选择/
Two things that hit me the most for SVG and Canvas were,
Ability to use Canvas without the DOM, where as SVG depends heavily on DOM and as the complexity increases the performance slows down. Like in game design.
Advantage of using SVG would be that resolution remains the same across platforms which lacks in Canvas.
Lot more detail is provided in this site. http://dev.opera.com/articles/view/svg-or-canvas-choosing-between-the-two/
这绝对取决于您的需要/要求。
如果您只想在屏幕上显示图像/图表,那么推荐
方法是画布。 (例如 PNG、GIF、BMP 等)
如果您想扩展图形的功能,例如如果您
将鼠标悬停在图表上想要缩放某些区域而不破坏
显示质量,然后选择 SVG。 (很好的例子是 AutoCAD、Visio、
GIS 文件)。
如果您想构建带有形状连接器的动态流程图创建工具,最好选择 SVG 而不是 CANVAS。
当屏幕尺寸增加时,画布开始退化
需要绘制更多像素。
当屏幕上的对象数量增加时,SVG 开始
当我们不断地将它们添加到 DOM 中时,它们就会降级。
另请参阅:http://msdn.microsoft .com/en-us/library/gg193983(v=vs.85).aspx
It is absolutely depends on your need/requirement.
If you want to just show an image/chart on a screen then recommended
approach is canvas. (Example is PNG, GIF, BMP etc.)
If you want to extend the features of your graphics for example if you
mouse over on the chart want to zoom certain area without spoil
display quality then you select SVG. (Good example is AutoCAD, Visio,
GIS files).
If you want build a dynamic flow diagram creator tool with shape connector it is better to select SVG rather than CANVAS.
When the size of the screen increases, canvas begins to degrade as
more pixels need to be drawn.
When the number of objects increases on the screen, SVG begins to
degrade as we are continually adding them to the DOM.
Also please refer : http://msdn.microsoft.com/en-us/library/gg193983(v=vs.85).aspx
SVG
基于用例 SVG 用于徽标、图表,因为您绘制了矢量图形,然后就忘记了它。当视口发生变化(例如重新调整大小(或缩放))时,它会自行调整,无需重绘。
画布
画布是位图(或光栅),它通过在屏幕上绘制像素来完成。它用于开发游戏或图形体验 (https://www.chromeexperiments.com/webgl)它在给定区域绘制像素并通过重新绘制另一个区域来更改。由于它是栅格类型,我们需要随着视口的变化而完全重绘。
参考
http://www .sitepoint.com/7-reasons-to-consider-svgs-instead-of-canvas
http ://en.wikipedia.org/wiki/WebGL
http://vector-conversions .com/vectorizing/raster_vs_vector.html
SVG
Based on use case SVG is used for logos, charts because its vector graphics you draw and forgot about it. When view port change like re-sizing(or zoom) it will adjust itself and no need to redraw.
Canvas
Canvas is bitmap (or raster) it done by painting of pixels to the screen. It is used to develop games or graphics experience (https://www.chromeexperiments.com/webgl) in a given area it paints pixel and changes by redraw it another. Since its a raster type we need to redraw entirely as view port changes.
Reference
http://www.sitepoint.com/7-reasons-to-consider-svgs-instead-of-canvas
http://en.wikipedia.org/wiki/WebGL
http://vector-conversions.com/vectorizing/raster_vs_vector.html
SVG
是一种绘图规范,类似于文件格式。 SVG 是一个文档。您可以像交换 HTML 文件一样交换 SVG 文件。此外,由于 SVG 元素和 HTML 元素共享相同的 DOM API,因此可以使用 JavaScript 生成 SVG DOM,就像创建 HTML DOM 一样。但您不需要 JavaScript 来生成 SVG 文件。一个简单的文本编辑器就足以编写 SVG。但你至少需要一个计算器来计算绘图中形状的坐标。CANVAS
只是一个绘图区域。需要使用 JavaScript 来生成画布的内容。您无法更换画布。它不是文件。并且画布的元素不是 DOM 树的一部分。您不能使用 DOM API 来操作画布的内容。相反,您必须使用专用的画布 API 将形状绘制到画布中。SVG
的优点是,您可以将绘图作为文档进行交换。 CANVAS 的优点是,它有一个不太冗长的 JavaScript API 来生成内容。下面是一个示例,它表明您可以实现类似的结果,但在 JavaScript 中执行此操作的方式非常不同。
正如您所看到的,结果几乎相同,但 JavaScript 代码完全不同。
SVG 是通过 DOM API 使用
createElement
、setAttribute
和appendChild
创建的。所有图形都在属性字符串中。 SVG 具有更强大的图元。例如,CANVAS 没有与 SVG 弧路径等效的东西。 CANVAS 示例尝试使用贝塞尔曲线模拟 SVG 弧线。在 SVG 中,您可以重用元素来转换它们。在 CANVAS 中您不能重复使用元素。相反,您必须编写一个 JavaScript 函数才能调用它两次。 SVG 有一个viewBox
,它允许使用标准化坐标,从而简化了旋转。在 CANVAS 中,您必须根据clientWidth
和clientHeight
自行计算坐标。您可以使用 CSS 设计所有 SVG 元素的样式。在 CANVAS 中,您无法使用 CSS 设置任何样式。由于 SVG 是 DOM,因此您可以将事件处理程序分配给所有 SVG 元素。 CANVAS 中的元素没有 DOM,也没有 DOM 事件处理程序。但另一方面,CANVAS 代码更容易阅读。您无需关心 XML 名称空间。而且你可以直接调用图形函数,因为你不需要构建DOM。
教训很明确:如果您想快速绘制一些图形,请使用画布。如果您需要共享图形,例如使用 CSS 设置样式或想要在图形中使用 DOM 事件处理程序,请构建 SVG。
SVG
is a specification of a drawing like a file format. A SVG is a document. You can exchange SVG files like HTML files. And additionally because SVG elements and HTML elements share the same the DOM API, it is possible to use JavaScript to generate a SVG DOM in the same way as it is possible to create a HTML DOM. But you do not need JavaScript to generate SVG file. A simple text editor is enough to write a SVG. But you need at least a calculator to calculate the coordinates of the shapes in the drawing.CANVAS
is just a drawing area. It it necessary to use JavaScript to generate the contents of a canvas. You can not exchange a canvas. It is no document. And the elements of the canvas are not part of the DOM tree. You can not use the DOM API to manipulate the contents of a canvas. Instead you have to use a dedicated canvas API to draw shapes into the canvas.The advantage of a
SVG
is, that you can exchange the drawing as a document. The advantage of theCANVAS
is, that is has a less verbose JavaScript API to generate the contents.Here is an example, which shows that you can achieve similar results, but the way how to do it in JavaScript is very different.
As you can see the result is almost the same, but the JavaScript code is completely different.
SVG is created with the DOM API using
createElement
,setAttribute
andappendChild
. All graphics are in the attribute strings. SVG has more powerful primitives. The CANVAS for example has nothing equivalent to the SVG arc path. The CANVAS example tries to emulate the SVG arc with a Bezier curve. In SVG you can reuse elements in order to transform them. In the CANVAS you can not reuse elements. Instead you have to write a JavaScript function in order to call it twice. SVG has aviewBox
which allows to use normalized coordinates, which simplifies rotations. In the CANVAS you have to calculate the coordinates yourself based of theclientWidth
andclientHeight
. And you can style all SVG elements with CSS. In the CANVAS you can not style anything with CSS. Because SVG is a DOM, you can assign event handlers to all SVG elements. The elements in the CANVAS have no DOM and no DOM event handlers.But on the other hand the CANVAS code is much easier to read. You do not need to care about XML name spaces. And you can directly call the graphics functions, because you do not need to build a DOM.
The lesson is clear: if you quickly want to draw some graphics, use the CANVAS. If you need to share the graphics, like to style it with CSS or want to use DOM event handlers in your graphics, build an SVG.
添加以上几点:
与 JPEG、GIF 等相比,SVG 是轻量级的,适合通过网络传输,并且在调整大小时可以极大地缩放而不会损失质量。
adding to the above points:
SVG is lightweight for transferring over web when compared to JPEG,GIF etc and also it scale extremely when resized without loosing the Quality.
SVG
画布元素
SVG
Canvas elements