校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > HTML5 Canvas概述

HTML5 Canvas概述

熱門標簽:高德地圖標注錯誤怎么修改 洛陽市伊川縣地圖標注中心官網 搜狗星級酒店地圖標注 平頂山電子地圖標注怎么修改 會聲會影怎樣做地圖標注效果 標準智能外呼系統 電銷機器人視頻 江蘇高頻外呼系統線路 地圖標注自己去過的地方

<canvas>是一個新的HTML元素,這個元素可以被Script語言(通常是JavaScript)用來繪制圖形。例如可以用它來畫圖、合成圖象、或做簡單的(和不那么簡單的)動畫。右面的圖象展示了一些<canvas>的應用示例,我們將會在此教程中看到他們的實現。

<canvas>最先在蘋果公司(Apple)的Mac OS X Dashboard上被引入,而后被應用于Safari。基于Gecko1.8的瀏覽器,例如Firefox 1.5,也支持這個新元素。元素<canvas>是WhatWG Web applications 1.0也就是大家都知道的HTML 5標準規范的一部分。

在本教程中,我將試著講述如何在你自己的網頁中使用<canvas>元素。提供的示例應該會給你些清晰概念,即用<canvas>能做些什么的。這些示例也可作為你應用<canvas>的起點。

開始使用之前
用元素<canvas>并不難,只要你具有HTML和 JavaScript的基礎知識。

如上所述,并不是所有現代瀏覽器都支持<canvas>元素,所以你需要 Firefox 1.5或更新版本、或者其他基于Gecko的瀏覽器例如Opera 9、或者最近版本的Safari才能看到所有示例的動作。

<canvas>元素

Let's start this tutorial by looking at the <canvas> element itself.
讓我們從<canvas>元素的定義開始吧。

<canvas id="tutorial" width="150" height="150"></canvas>

This looks a lot like the <img> element, the only difference is that it doesn't have the src and alt attributes. <canvas>看起來很像<img>,唯一不同就是它不含 srcalt 屬性。The <canvas> element has only two attributes - width and height. These are both optional and can also be set using DOM properties or CSS rules.它只有兩個屬性,widthheight,兩個都是可選的,并且都可以 DOM 或者 CSS 來設置。 When no width and height attributes are specified, the canvas will initially be 300 pixels wide and 150 pixels high.如果不指定width 和 height,默認的是寬300像素高150像素。The element can be sized arbitrarily by CSS, but during rendering the image is scaled to fit its layout size.   (If your  renderings seem distorted, try specifying your width and height attributes explicitly in the <canvas> attributes, and not with CSS.)雖然可以通過 CSS 來調整canvas的大小,但渲染圖像會縮放來適應布局的(如果你發現渲染結果看上去變形了,不必一味依賴CSS,可以嘗試顯式指定canvas的width 和 height 屬性值)。

The id attribute isn't specific to the <canvas> element but is one of default HTML attributes which can be applied to (almost) every HTML element (like class for instance). It's always a good idea to supply an id because this makes it much easier to identify it in our script.
id  屬性不是<canvas>專享的,就像標準的HTLM標簽一樣,任何一個HTML元素都可以指定其 id 值。一般,為元素指定 id 是個不錯的主意,這樣使得在腳本中應用更加方便。

The <canvas> element can be styled just like any normal image (margin, border, background, etc). These rules however don't affect the actual drawing on the canvas. We'll see how this is done later in this tutorial. When no styling rules are applied to the canvas it will initially be fully transparent. <canvas>元素可以像普通圖片一樣指定其樣式(邊距,邊框,背景等等)。然而這些樣式并不會對canvas實際生成的圖像產生什么影響。下面我們會看到如何應用樣式。如果不指定樣式,canvas默認是全透明的。

替用內容

Because the <canvas> element is still relatively new and isn't implemented in some browsers (such as Firefox 1.0 and Internet Explorer), we need a means of providing fallback content when a browser doesn't support the element.

因為 <canvas> 相對較新,有些瀏覽器并沒實現,如Firefox 1.0 和 Internet Explorer,所以我們需要為那些不支持canvas的瀏覽器提供替用顯示內容。

Luckily this is very straightforward: we just provide alternative content inside the canvas element. Browsers who don't support it will ignore the element completely and render the fallback content, others will just render the canvas normally.
For instance we could provide a text description of the canvas content or provide a static image of the dynamically rendered content. This can look something like this:

我們只需要直接在canvas元素內插入替用內容即可。不支持canvas的瀏覽器會忽略canvas元素而直接渲染替用內容,而支持的瀏覽器則會正常地渲染canvas。例如,我們可以把一些文字或圖片填入canvas內,作為替用內容:

<canvas id="stockGraph" width="150" height="150">
  current stock price: $3.15 +0.15
</canvas>

<canvas id="clock" width="150" height="150">
  <img src="images/clock.png" width="150" height="150"/>
</canvas>

結束標簽 </canvas> 是必須的

In the Apple Safari implementation, <canvas> is an element implemented in much the same way <img> is; it does not have an end tag. However, for <canvas> to have widespread use on the web, some facility for fallback content must be provided. Therefore, Mozilla's implementation requires an end tag (</canvas>).

在Apple Safari里,<canvas>的實現跟<img>很相似,它并不沒有結束標簽。然而,為了使 <canvas> 能在web的世界里廣泛適用,需要給替用內容提供一個容身之所,因此,在Mozilla的實現里結束標簽(</canvas>)是必須的。

If fallback content is not needed, a simple <canvas id="foo" ...></canvas> will be fully compatible with both Safari and Mozilla -- Safari will simply ignore the end tag.

如果沒有替用內容,<canvas id="foo" ...></canvas> 對 Safari 和 Mozilla 是完全兼容的—— Safari 會簡單地忽略結束標簽。

If fallback content is desired, some CSS tricks must be employed to mask the fallback content from Safari (which should render just the canvas), and also to mask the CSS tricks themselves from IE (which should render the fallback content).

如果有替用內容,那么可以用一些 CSS 技巧來為并且僅為 Safari 隱藏替用內容,因為那些替用內容是需要在 IE 里顯示但不需要在 Safari 里顯示。

渲染上下文(Rendering Context)

<canvas> creates a fixed size drawing surface that exposes one or more rendering contexts, which are used to create and manipulate the content shown. We'll focus on the 2D rendering context, which is the only currently defined rendering context. In the future, other contexts may provide different types of rendering; for example, it is likely that a 3D context based on OpenGL ES will be added.

<canvas> 創建的固定尺寸的繪圖畫面開放了一個或多個渲染上下文(rendering context),我們可以通過它們來控制要顯示的內容。我們專注于2D 渲染上,這也是目前唯一的選擇,可能在將來會添加基于OpenGL ES 的 3D 上下文。

The <canvas> is initially blank, and to display something a script first needs to access the rendering context and draw on it. The canvas element has a DOM method called getContext, used to obtain the rendering context and its drawing functions. getContext() takes one parameter, the type of context.

<canvas> 初始化是空白的,要在上面用腳本畫圖首先需要其渲染上下文(rendering context),它可以通過 canvas 元素對象的 getContext 方法來獲取,同時得到的還有一些畫圖用的函數。getContext() 接受一個用于描述其類型的值作為參數。

var canvas = document.getElementById('tutorial');
var ctx = canvas.getContext('2d');

In the first line we retrieve the canvas DOM node using the getElementById method. We can then access the drawing context using the getContext method.

上面第一行通過 getElementById 方法取得 canvas 對象的 DOM 節點。然后通過其 getContext 方法取得其畫圖操作上下文。

檢查瀏覽器的支持

The fallback content is displayed in browsers which do not support <canvas>; scripts can also check for support when they execute. This can easily be done by testing for the getContext method. Our code snippet from above becomes something like this:

除了在那些不支持  的瀏覽器上顯示替用內容,還可以通過腳本的方式來檢查瀏覽器是否支持 canvas 。方法很簡單,判斷 getContext 是否存在即可。

var canvas = document.getElementById('tutorial');
if (canvas.getContext){
  var ctx = canvas.getContext('2d');
  // drawing code here
} else {
  // canvas-unsupported code here
}

代碼模板

Here is a minimalistic template, which we'll be using as a starting point for later examples. You can download this file to work with on your system.

我們會用下面這個最簡化的代碼模板來(后續的示例需要用到)作為開始,你可以 下載文件 到本地備用。

<html>
  <head>
    <title>Canvas tutorial</title>
    <script type="text/javascript">
      function draw(){
        var canvas = document.getElementById('tutorial');
        if (canvas.getContext){
          var ctx = canvas.getContext('2d');
        }
      }
    </script>
    <style type="text/css">
      canvas { border: 1px solid black; }
    </style>
  </head>
  <body onload="draw();">
    <canvas id="tutorial" width="150" height="150"></canvas>
  </body>
</html>

If you look at the script you'll see I've made a function called draw, which will get executed once the page finishes loading (via the onload attribute on the body tag). This function could also have been called from a setTimeout, setInterval, or any other event handler function just as long the page has been loaded first.

細心的你會發現我準備了一個名為 draw 的函數,它會在頁面裝載完畢之后執行一次(通過設置 body 標簽的 onload 屬性),它當然也可以在 setTimeout,setInterval,或者其他事件處理函數中被調用。

一個簡單的例子

To start off, here's a simple example that draws two intersecting rectangles, one of which has alpha transparency. We'll explore how this works in more detail in later examples.

作為開始,來一個簡單的吧——繪制兩個交錯的矩形,其中一個是有alpha透明效果。我們會在后面的示例中詳細的讓你了解它是如何運作的。

<html>
 <head>
  <script type="application/x-javascript">
    function draw() {
      var canvas = document.getElementById("canvas");
      if (canvas.getContext) {
        var ctx = canvas.getContext("2d");

        ctx.fillStyle = "rgb(200,0,0)";
        ctx.fillRect (10, 10, 55, 50);

        ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
        ctx.fillRect (30, 30, 55, 50);
      }
    }
  </script>
 </head>
 <body onload="draw();">
   <canvas id="canvas" width="150" height="150"></canvas>
 </body>
</html>

標簽:果洛 廣西 常德 蚌埠 鄂爾多斯 廣東 阿克蘇 松原

巨人網絡通訊聲明:本文標題《HTML5 Canvas概述》,本文關鍵詞  HTML5,Canvas,概述,HTML5,Canvas,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《HTML5 Canvas概述》相關的同類信息!
  • 本頁收集關于HTML5 Canvas概述的相關信息資訊供網民參考!
  • 推薦文章
    校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃
    欧美午夜不卡在线观看免费| 国产高清精品在线| 国产精品国产三级国产aⅴ无密码| 欧美午夜精品一区| 欧美在线观看你懂的| 91亚洲精品一区二区乱码| 成人av电影在线网| 99久久亚洲一区二区三区青草| 成人av资源网站| 91美女视频网站| 欧美色偷偷大香| 91精品国产综合久久婷婷香蕉| 9191国产精品| 久久久久久综合| 国产精品免费av| 亚洲最新视频在线观看| 亚洲成年人网站在线观看| 美女一区二区在线观看| 高清在线观看日韩| 欧美日韩亚洲不卡| 精品美女一区二区| 国产精品美女久久久久久2018| 国产精品国产三级国产aⅴ中文| 一区二区三区不卡在线观看| 午夜av区久久| 国产成人a级片| 欧美午夜片在线看| 久久综合国产精品| 亚洲黄色免费电影| 精品无人区卡一卡二卡三乱码免费卡| 国产精品91xxx| 欧美日韩三级在线| 中文字幕高清不卡| 日韩成人精品在线| av在线播放一区二区三区| 精品污污网站免费看| 久久亚洲欧美国产精品乐播| 亚洲欧美激情插| 韩国三级中文字幕hd久久精品| aaa国产一区| 欧美成人vps| 亚洲午夜精品在线| 国产成人免费在线观看不卡| 欧美日韩国产bt| 国产精品久久久久久久蜜臀| 久久99精品久久久久婷婷| 日本韩国一区二区三区| 精品理论电影在线| 亚洲成人先锋电影| 91在线你懂得| 国产欧美日韩三级| 久久99久国产精品黄毛片色诱| 欧美曰成人黄网| 国产精品不卡一区| 成人夜色视频网站在线观看| 欧美一区二区三区视频在线观看| 亚洲免费大片在线观看| 成人丝袜视频网| 欧美精品一区二区三区久久久| 日韩精品久久理论片| 在线免费观看日本一区| 国产精品久久久久久久久免费樱桃| 免费在线视频一区| 337p亚洲精品色噜噜狠狠| 夜夜精品视频一区二区| 91日韩精品一区| 亚洲视频一区二区免费在线观看| 国产suv精品一区二区883| 久久综合网色—综合色88| 麻豆国产精品一区二区三区 | 欧美喷水一区二区| 综合电影一区二区三区| 99久久亚洲一区二区三区青草| 国产欧美日韩综合| 国产91精品一区二区| 久久久久久久久久久黄色| 国产美女精品人人做人人爽| 久久久久久久免费视频了| 国产精品一区二区男女羞羞无遮挡| 日韩欧美成人一区二区| 国产永久精品大片wwwapp| 久久女同互慰一区二区三区| 国产精品一二三四五| 国产无遮挡一区二区三区毛片日本| 国产在线观看一区二区| 国产拍揄自揄精品视频麻豆| 成人av免费观看| 一区二区三区精品在线| 在线播放91灌醉迷j高跟美女 | 麻豆91在线观看| 欧美r级电影在线观看| 国产一区二区三区免费| 国产精品乱人伦一区二区| 欧亚一区二区三区| 久久99精品国产.久久久久久| 久久女同精品一区二区| 不卡的av在线播放| 午夜激情一区二区| 欧美tickling网站挠脚心| 国产91精品入口| 亚洲线精品一区二区三区八戒| 欧美精选午夜久久久乱码6080| 理论电影国产精品| 日韩一区在线看| 555夜色666亚洲国产免| 成人做爰69片免费看网站| 亚洲黄色在线视频| 久久美女高清视频| 欧美亚洲综合一区| 国产精品一区二区久久不卡| 亚洲欧美自拍偷拍| 91精品国产一区二区三区蜜臀 | 99免费精品在线| 亚洲国产另类av| 久久久亚洲综合| 日本久久一区二区三区| 国产一区二区精品久久99| 一区二区三国产精华液| 久久久久久久综合日本| 欧美日韩中文字幕一区二区| 国产成人精品免费视频网站| 天堂精品中文字幕在线| 中文字幕的久久| 久久综合中文字幕| 欧美美女直播网站| 91麻豆swag| a级高清视频欧美日韩| 久久成人免费日本黄色| 亚洲无人区一区| 亚洲视频综合在线| 久久青草国产手机看片福利盒子| 欧美视频一区在线| www.欧美精品一二区| 国产一区二区中文字幕| 免费高清成人在线| 午夜精品久久久久久久| 亚洲乱码中文字幕综合| 久久精品一区二区三区不卡牛牛| 69p69国产精品| 欧美人伦禁忌dvd放荡欲情| 91在线高清观看| 99国产精品国产精品久久| 丁香五精品蜜臀久久久久99网站| 九色porny丨国产精品| 日韩成人午夜精品| 日韩高清中文字幕一区| 一区二区三国产精华液| 亚洲欧美日韩国产另类专区| 亚洲欧洲成人自拍| 成人免费一区二区三区视频 | 不卡一区二区三区四区| 国产成人丝袜美腿| 成人免费高清在线| 波多野结衣91| 色欧美乱欧美15图片| 一本大道久久a久久精品综合| fc2成人免费人成在线观看播放| 国产白丝网站精品污在线入口| 国产毛片精品国产一区二区三区| 精久久久久久久久久久| 国产99久久久国产精品| 成人永久免费视频| 成人开心网精品视频| av一本久道久久综合久久鬼色| 91首页免费视频| 色久综合一二码| 777a∨成人精品桃花网| 欧美v国产在线一区二区三区| 久久久99精品久久| 中文字幕欧美一区| 亚洲一区二三区| 奇米影视一区二区三区| 激情深爱一区二区| 成人av网站在线观看免费| 色视频欧美一区二区三区| 91精品久久久久久久99蜜桃 | 在线观看区一区二| 3d成人h动漫网站入口| 久久久久久亚洲综合| 亚洲嫩草精品久久| 久久爱另类一区二区小说| www.欧美色图| 日韩视频一区二区在线观看| 欧美国产一区在线| 视频在线在亚洲| 懂色av噜噜一区二区三区av| 在线观看区一区二| 久久久精品中文字幕麻豆发布| 一片黄亚洲嫩模| 国产成人在线电影| 91精品麻豆日日躁夜夜躁| 国产精品美女一区二区三区| 日韩在线一区二区三区| gogogo免费视频观看亚洲一| 91麻豆精品国产无毒不卡在线观看| 国产精品视频一二三| 蜜桃一区二区三区在线| 色噜噜久久综合| 国产亚洲欧美在线| 日韩专区中文字幕一区二区|