原文:dom_vs_canvas

在web上做动画我们有很多选择:svg,dom,canvas,flash等等。但是根据业务场景我们一定能找到最优的技术解决方案,但是何时选择dom,何时使用canvas呢,以及2者的区别是什么呢。是选择能快速开发的dom,还是选择对性能消耗较小的canvas呢。下文从浏览器渲染方面对技术选型做了解释。

Did you know that you have a choice in how you can get things to display in your browser? Well...you do! The most common approach for getting content to display on your screen is by working with DOM elements. This is where you (and 99% of the entire world) create HTML, CSS, and JavaScript and have elements appear magically. The other approach uses the canvas element. With a canvas, you manually specify exactly what you want drawn and displayed on your screen. Both of these approaches have their uses. For the kinds of visually complex applications you will be creating, knowing when to use which is a good thing for you to be aware of.

To help you with this, let's take a step back. In fact, let's take many steps back and look at how our two approaches map to how your browser translates what you want into something that gets displayed. Core to this translation are two modes called Retained Mode and Immediate Mode. While it may not seem like it right now, understanding the details of both of these modes will help you know when to rely on the DOM APIs and when to use canvas to display visuals on the screen.

为了让你理解这些,让我们回到很早以前看看这2种转换方法是如何将你要展示的内容显示到浏览器中的。这个命令的转换的核心有2种:驻留模式和快速模式。我解释的虽然现在看起来不太准确,但是理解这2种转换的细节将会帮助你知道你何时该使用DOM、何时该使用canvas来完成你的图形展示。

Onwards!

让我们继续

Retained Mode (DOM)

驻留模式

In a retained mode system, the way you get things to display on the screen is by sending your hopes, dreams, and desires to your browser's Graphics API. This API, much like Santa Claus, gives you whatever you ask for.

在驻留模式中,你通过浏览器图形化接口让显示器显示你想展示的东西。这个接口就像圣诞老人,给你你想要的。

The following diagram roughly describes the division of labor between you, the Graphics API, and your browser:

下面的图片粗略的描述了你、图形化接口、浏览器之间的工作分割

The yellow box represents the HTML, CSS, and JavaScript that makes up your application. While you may not have thought about your markup and code in quite this way, almost everything you specify is nothing more than a drawing instruction that tells your browser what to display on the screen.

黄色盒子代表构建你应用的html、css和js。虽然你可能没有想过这个api的具体实现,但是api帮助你完成你定义的结构,然后转换,然后将转换展示到显示器上。

This translation between your raw markup and code to something visual is handled by your browser's Graphics API. This API takes what you specify and creates an in-memory model (often referred to as a scene, object list or display list) of what the final output should look like. Once it creates this model, the final step is to translate the model into the arcane draw commands that your browser understands.

你的原始标记代码变成可视化图形是通过图形接口来转换的。图形化接口需要你指定和创建一个存在内存的模型(我们可以称之为场景,对象列表和显示列表),这个模型最终会输出一个你输入的图像。一旦api创建了这个模型,那么最后一步就是api就会把模型表述的图像通过浏览器能理解的命令画在你的浏览器。

All of this happens very seamlessly behind the scenes. You simply define your application. The details of getting what you defined into something you can see is automatically handled for you.

所以的这一切都是在后台无缝的完成。你只需要写好html,css,js定义好你的app。api会自动帮你处理好你定义的东西。

Immediate Mode (Canvas)

快速模式

Contrasting the gentle comforts of a retained mode system is the immediate mode one where...well, let's just look at the diagram first:

相对于使用起来很舒服的驻留模式就是立即模式,让我先看看下面这张图:

In an immediate mode system, you do all of the heavy lifting. You not only specify what needs to be drawn, you also create and maintain the model as well. As if all of this wasn't enough, you also specify the draw commands to get your browser to actually update! Your browser's Graphics API, the API that did so much for you in the retained mode world, doesn't do much here. It simply takes your draw commands and sends them off to the browser for execution.

在立即模式系统中,你的工作量有很大的提升。你不仅需要指定什么需要画,你还要创建和维护一个模型。

In HTML, immediate mode comes into play when you are using the canvas element. Anything you wish to draw inside it requires you to issue primitive draw commands and handle all aspects of managing the scene and redrawing when changes happen.

在html中canvas的渲染就是使用快速模式。在快速模式中,你想要画的东西是通过原始的画图命令并且你要管理好当前场景和重绘时带来的改变。

When to Use Which?

如何抉择

Whenever a section heading promises a clear answer, you know that the answer is never clear. This time is no exception. Choosing between the immediate mode-ness of the canvas and the retained mode-ness of your DOM is not an exclusive decision. It can actually be quite scandalous! You can choose just one, the other, or even both.

In this section, let's build on the overview you saw in the previous section and look at the advantages and disadvantages of each of the approaches.

在这个部分,让我们建立在你以前看的概述部分,看看2种技术实现的优点和缺点。

The DOM

The DOM

Since we are talking about the DOM here, you will spend a majority of the time, possibly even all of your time, in this retained mode world. Despite the comforts it provides, it isn't perfect.

既然我们开始探讨dom了,那么我们将会在驻留模式中花较多时间,或者更多时间来理解它。

Let's look at its perfections and imperfections in more detail:

让我们来详细看看dom的优点和缺点

  1. Easy to use. The DOM abstracts away a lot of the details that would otherwise get you bogged down. Examples of details that can bog down even the best of us include layout, event handling, clean up, selection/highlighting, accessibility, being DPI friendly, and so on.
  2. Redrawing is handled for you. You only specify what you want to display on the screen. The details of how to do that and how often to refresh are all left to the Graphics API to handle.
  3. CSS! CSS! CSS! You can easily modify the visuals of your DOM elements using CSS.
  4. Animations are easy to define and modify. Because of the CSS support, you can easily define animations or transitions, specify an easing function, make a few other tweaks, and you are good to go. This applies to JavaScript-based animations as well. If you are using JavaScript to animate an element's properties, you just have to get your requestAnimationFrame loop setup to update the property values. Everything else is taken care off...such as when to redraw or how to maintain a smooth frame rate.
  5. Memory intensive. You know all of the details that get taken care of for you when using a DOM element? Well, that care doesn't come cheap. Your DOM elements are very complex little things, and all of this complexity takes up space in your browser's memory. The more elements you are dealing with, the more resource hungry it all gets.
  6. Less control over how things get drawn. For certain graphics-related tasks, the default rendering may be a bit limiting. Browsers optimize for their particular needs, and those optimizations may go counter to what you want to do.

  7. 使用简单。DOM封装了很多细节出来以让你简单使用。比如布局,事件,移除,选择,可访问,甚至可以像素级别的控制,等等
  8. 你可以控制重绘。你只需要在显示器指定你希望展示的内容。而展示的细节和刷新频率完全是交给api来完成的。
  9. 你可以简单的通过css来改变dom的展示样式
  10. 动画是很容易定义和修改的。通过css你可以很容易的定义动画和变形,或者可以指定一个动画函数。当然通过js来完成动画也是可以的。如果你使用js修改元素属性来完成动画,你只需要通过requestAnimationFrame循环来修改属性值。而其他的一切都是给api来完成...比如什么做重绘或者如何做到平滑的动画帧率。
  11. 内存消耗。你确定当你使用dom的时候你知道api的实现细节?api帮你做的事可不是很便宜。你的dom节点是一个很复杂的小东西,而复杂性和内存消耗成正比。你需要的dom越多,就越吃内存。
  12. 无法控制绘画的细节。在某些绘画场景中,默认的渲染可能有很多限制。浏览器会做出一些它自己的优化,而这个优化可能与你想做的事相反

The Canvas

If this were a popularity contest, I would feel pretty bad for immediate mode and the canvas element that uses it. Fortunately, it isn't! Immediate mode systems certainly carry their own weight - even in the more limited cases they are used in.

如果这是一个流行的比赛,我对canvas元素使用立即模式感到很糟糕。幸运的是,canvas并不是这样。

Let's look at some of their cool (and less cool) features in more detail:

让我们来详细看看它很酷(和一些不酷)的特性。

  1. Fast. Really fast. Because an immediate mode system doesn't maintain its own model, your code is all that stands between you and the browser redrawing. The many layers of abstraction that slow operations down simply do not exist in the immediate mode world.
  2. You have a lot of flexibility. Since your code controls all aspects of when and how something is drawn to the screen, you can tweak and customize the output any way you would like.
  3. Great for dealing with many elements. Compared to a retained mode system where every little addition to your scene takes up extra memory, immediate mode systems don't have that problem. Generally, an immediate mode system will always use less memory than a retained mode system - something that becomes more noticeable as you add more and more elements into the mix.
  4. It can be slow when drawing to large areas. How quickly a redraw completes is proportional to the number of pixels you are re-drawing. If your addressing a really large area, things could get slower if you are not careful and do not optimize appropriately.
  5. It is complex. Because you are handling more of what it takes to get something to display on the screen, there are a lot more details for you to keep track of. Getting up to speed with the various draw commands and how they are used is no picnic either.
  6. 快速、真的很快。因为在立即模式中,它不需要维护视图模型,所以的浏览器重绘都是基于你的代码。在立即模式中不存在能让处理变慢的多层次抽象。
  7. 灵活性很高。一旦你可以通过你的代码可以方方面面何时绘制到屏幕,那么你可以用你喜欢的渲染方式想怎么画就怎么画
  8. 可以控制更多的元素。相比驻留模式中的每增加一点东西到场景中就要消耗一些内存,及时模式并没有这个问题。一般来说,在立即模式中消耗的内存比驻留模式少——在你添加了很多元素时这种现象越发的明显。
  9. 当你需要展示一个很大的图像时,性能就会变的很差。因为重绘的复杂度和你图像的像素成正相关。如果你不够仔细并且不做优化,当你画越大图像你会渲染的越慢
  10. 复杂。随着你需要处理更多的展示内容,你就越要关注图像渲染细节。快速掌握各种绘图命令和它们是如何使用的是不是件容易的事。

Summary

总结

Understanding the retained mode and immediate mode differences makes it much easier to sympathize with the DOM on certain things and with the canvas on others. By now, hopefully you have a good idea of when to use one over the other. In case it helps, here is my short list on when I use canvas and when I use the DOM.

理解了保留模式和直接模式的差异,会让你更简单的区分何时该使用dom,何时该使用canvas。到现在为止,希望你能明白如何抉择。如果我所讲的对你有帮助,那么我对何时用dom何时用canvas做了一个简短列表

When I Use a Canvas

我何时选择canvas

When I Use the DOM

我何时选择DOM

This is pretty simple. I use the DOM for everything else that I don't use a canvas for. The canvas has very limited uses for the kinds of things that I do.

这很简单,只要我不用canvas我就用dom完成动画。在很多我的产品实现中canvas有很大局限性。

Dom vs Canvas (译)的更多相关文章

  1. 译:DOM2中的高级事件处理(转)

    17.2. DOM2中的高级事件处理(Advanced Event Handling with DOM Level 2)        译自:JavaScript: The Definitive Gu ...

  2. canvas 动态画线

    <!--实现鼠标按下的时候,移动进行绘制,鼠标抬起结束绘图,用到的事件有mousedown mousemove mouseup用的的canvas api 有 beginPath moveTo l ...

  3. Html5 Canvas transform setTransform

    Html5 Canvas transform就是矩阵变换,一种坐标的变形. 坐标变形的三种方式,平移translate, 缩放scale以及旋转rotate都可以通过transform做到. tran ...

  4. [置顶] High Performance Canvas Game for Android

    Rule #0 为移动平台进行优化 为移动平台进行优化是十分重要的,因为移动平台的性能大概只有桌面平台的1/10左右(*1),它通常意味着: 更慢的CPU速度,这意味着不经过优化的JavaScript ...

  5. canvas 弹幕效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. canvas图表详解系列(4):动态散点图

    本章建议学习时间4小时 学习方式:详细阅读,并手动实现相关代码(如果没有canvas基础,需要先学习前面的canvas基础笔记) 学习目标:此教程将教会大家如何使用canvas绘制各种图表,详细分解步 ...

  7. canvas图表详解系列(5):雷达(面积)图

    雷达(面积)图 本章建议学习时间4小时 学习方式:详细阅读,并手动实现相关代码(如果没有canvas基础,需要先学习前面的canvas基础笔记) 学习目标:此教程将教会大家如何使用canvas绘制各种 ...

  8. js canvas游戏初级demo-躲避障碍物

    在线演示地址 http://200ok.fun:3100/html/game_demo.html 继上次js canvas游戏初级demo-上下左右移动(https://www.cnblogs.com ...

  9. js canvas游戏初级demo-上下左右移动

    大概流程就是监听状态变化擦除画布重绘 由于js监听时间变化的函数addEventListener只能达到每秒触发20次左右,也就是每秒20帧,看起来有点卡卡的 所以用定时器搞到每秒30帧 按上下左右键 ...

随机推荐

  1. ssh登录慢的解决办法

    ubuntu的ssh登录有点慢,其实是很慢 google了一把,发现可以这样解决: (1)可能是DNS反向解析的问题 对于这样的问题,可以在/etc/ssh/sshd_config 中添加/修改: U ...

  2. sql 时间相关

    1.常用日期方法(下面的GetDate() = '2006-11-08 13:37:56.233') (1)DATENAME ( datepart ,date ) 返回表示指定日期的指定日期部分的字符 ...

  3. cocos2dx-3.0(8)------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法

    ----我的生活,我的点点滴滴!! 最后一个LabelBMFont了,字体图集LabelBMFont,LabelBMFont类是一个基于位图的字体图集.是一个包括全部你须要于坐标数据一起显示在屏幕上的 ...

  4. 38: 立方根getCubeRoot

    题目描述:计算一个数字的立方根,不使用库函数 •接口说明 原型:public static double getCubeRoot(double input) 输入:double 待求解参数 返回值:d ...

  5. 【Java项目实战】——DRP之HTML总结

    在DRP的学习之中,又将之前BS的内容又一次复习了一遍,借着复习的机会将BS的各个部分再又一次总结一下.今天来总结一下HTML. 在学习BS之后就进入了权限系统的开发之中,可是仍然发现非常多代码不会不 ...

  6. 10个经典Java面试题

    1.Java的HashMap是怎样工作的? HashMap是一个针对数据结构的键值.每一个键都会有对应的值.关键是识别这种值. HashMap 基于 hashing 原理,我们通过 put ()和 g ...

  7. 理解cas

    前言 CAS(Compare and Swap),即比较并替换,实现并发算法时常用到的一种技术,Doug lea大神在java同步器中大量使用了CAS技术,鬼斧神工的实现了多线程执行的安全性. CAS ...

  8. Windows+VS+SVN实现版本控制

    Subversion已经是一个热门话题,下面介绍一下Windows下Subversion和TortoiseSVN构建SVN版本控制 问题. 首先看一些基础知识: Subversion是架设一个SVN ...

  9. Python常用变量处理手记(拼接数字,转json)

    1.拼接字符串和数字时,应先把数字做转换 如,bytes(page) 再做拼接:str+page 或者 s = 'abc' print s + str(1) #abc1 使用list和tuple 参考 ...

  10. react build和server start

    先到项目目录build项目 npm run build 项目会打包到dist文件夹下 index.html和index.js等 react的项目build后不能直接访问的问题 先执行 npm inst ...