简体   繁体   English

SVG与CANVAS(Snap.svg vs FabricJS)

[英]SVG vs CANVAS (Snap.svg vs FabricJS)

I made a speedtest to compare Snap.svg (SVG) to FabricJS (CANVAS): http://jsbin.com/tadec/7 function dummy() . 我做了一个速度来比较Snap.svg(SVG)和FabricJS(CANVAS): http ://jsbin.com/tadec/7 function dummy()

In Chrome SVG renders in 120 ms, while CANVAS renders in 1100 ms. 在Chrome中SVG渲染时间为120毫秒,而CANVAS渲染时间为1100毫秒。 SVG is 9x faster than CANVAS. SVG比CANVAS快9倍。

Fabricjs.com page says in this example that Raphael takes 225 ms and Fabric takes 97 ms (parsing: 80, rendering: 17). Fabricjs.com页面在这个例子中说拉斐尔需要225毫秒而Fabric需要97毫秒(解析:80,渲染:17)。

I have had an impression (after reading fabricjs.com and paperjs.org ) that FabricJS and more generally Canvas is faster than SVG. 我有一个印象(在阅读fabricjs.com和paperjs.org之后 ),FabricJS和更常见的Canvas比SVG更快。

My speed test claims that SVG is significantly faster than Canvas (at least Snap.svg seems to be significantly faster than FabricJS). 我的速度测试声称SVG明显快于Canvas(至少Snap.svg似乎比FabricJS快得多)。

Why FabricJS is so slow in my test? 为什么FabricJS在我的测试中如此之慢? Have I made some mistake in comparison, because I'm surprised that SVG seems to be faster than Canvas in my speed test. 相比之下,我是否犯了一些错误,因为我很惊讶在我的速度测试中SVG似乎比Canvas快。

EDIT: My question is two-parted: Why rendering speed is so much slower in FabricJS and why dragging speed as well? 编辑:我的问题是两部分:为什么FabricJS中的渲染速度要慢得多,为什么拖动速度呢?

Your benchmark is broken in my opinion, because beside measuring drawing to canvas you are measuring parsing of a huge string containing a path, over and over again. 在我看来,你的基准测试被打破了,因为除了测量绘图到画布之外,你正在测量一个包含路径的大字符串的解析,一遍又一遍。 Separate this code out of the loop and you should get more reliable results. 将此代码分离出循环,您应该获得更可靠的结果。

Measurements that are provided for canvas libraries are provided for drawing, not for parsing or other pre-processing work. 为画布提供的测量是为绘图而提供的,而不是用于解析或其他预处理工作。 If you use canvas like you use SVG, then yes, it will be slower. 如果您像使用SVG一样使用画布,那么是的,它会更慢。 It is not intended to be used like SVG. 它不打算像SVG一样使用。 FabricJS provides a way to do that, but it is not optimal. FabricJS提供了一种方法,但它不是最佳选择。 One solution would be to parse path once, and then use same path over and over instead of parsing it every time. 一种解决方案是解析路径一次,然后反复使用相同的路径而不是每次解析它。

Also, measurements are given probably for drawing a canvas, not for interaction with parts. 此外,测量可能用于绘制画布,而不是与零件的交互。 As you say in comments, rendering may be improved, but why does dragging a shape take so much longer? 正如你在评论中所说,渲染可能会有所改进,但为什么拖动一个形状会花费更长时间? Because: 因为:

  1. maybe path is being reparsed on each redraw (not sure how FabricJS works) 也许路径正在重新绘制每个重绘(不确定FabricJS如何工作)
  2. because SVG can redraw only certain parts of image that you are moving, and canvas is usually redrawn completely. 因为SVG只能重绘您正在移动的图像的某些部分,并且画布通常会完全重绘。 Why? 为什么? Because you can't "erase" part of canvas where a shape used to be. 因为你无法“擦除”曾经是形状的画布的一部分。 So entire canvas is erased, and new positions are redrawn. 因此整个画布被删除,并重新绘制新的位置。

Why do then people say canvas is faster than SVG for such scenarios? 为什么人们说这种情况下画布比SVG更快? Because it is if you use it properly. 因为如果你正确使用它。 It will be more work, but it will work much faster. 这将是更多的工作,但它将更快地工作。

  1. Don't use SVG paths for drawing shapes, or use simple paths and cache them 不要使用SVG路径绘制形状,或使用简单路径并缓存它们
  2. Cache shapes which you use often into off-screen (hidden canvas) and then copy from that canvas onto visible canvas when needed 缓存您经常在屏幕外(隐藏的画布)使用的形状,然后在需要时从该画布复制到可见的画布上
  3. If you have multiple independant layers of an image (for example 3 layers in a game, if you have background sky which is moving, background mountains which are moving slower and a character), use multiple canvases. 如果你有一个图像的多个独立层(例如游戏中有3个图层,如果你有移动的背景天空,移动速度较慢的背景山和一个角色),请使用多个画布。 Put canvases one over another, draw sky on the bottom canvas, draw mountains on second canvas and draw character on top canvas. 将画布一个放在另一个上,在底部画布上绘制天空,在第二个画布上绘制山峰并在顶部画布上绘制角色。 That way, when character on top canvas moves, you don't have to redraw entire background. 这样,当顶部画布上的角色移动时,您不必重绘整个背景。

I hope my answer is useful for you :) 我希望我的回答对你有用:)

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

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