亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > HTML5 > 正文

HTML5 Canvas概述

2024-09-05 07:19:43
字體:
來源:轉載
供稿:網友

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

<canvas>最先在蘋果公司(Apple)的Mac OS X Dashboard上被引入,而后被應用于Safari?;贕ecko1.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>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲人在线视频| 性色av香蕉一区二区| 26uuu另类亚洲欧美日本一| 亚洲天堂成人在线视频| 中文字幕精品在线视频| 国模视频一区二区三区| 国产成人综合精品在线| 成人看片人aa| 欧美精品制服第一页| 精品人伦一区二区三区蜜桃免费| 亚洲人成在线观看| 欧美怡春院一区二区三区| 色天天综合狠狠色| 欧美性极品少妇精品网站| 国产精品自产拍在线观看中文| 成人免费大片黄在线播放| 精品久久久久久中文字幕大豆网| 北条麻妃久久精品| 国产a∨精品一区二区三区不卡| 欧美成人性生活| 综合网中文字幕| 亚洲精品97久久| 日韩在线欧美在线国产在线| 国色天香2019中文字幕在线观看| 91中文在线视频| 在线观看免费高清视频97| 久久久久免费精品国产| 中文字幕精品久久久久| 日韩美女免费观看| 亚洲jizzjizz日本少妇| www.日韩不卡电影av| 深夜精品寂寞黄网站在线观看| 国产精品爱啪在线线免费观看| 国产亚洲福利一区| 国产裸体写真av一区二区| 视频在线一区二区| 欧洲亚洲免费视频| 亚洲人成在线观看网站高清| 色噜噜狠狠狠综合曰曰曰88av| 国产亚洲精品久久久久动| 日韩精品在线免费播放| 日日噜噜噜夜夜爽亚洲精品| 91精品国产综合久久久久久久久| 欧美极品少妇xxxxⅹ免费视频| 久久久亚洲影院| 国产欧美日韩综合精品| 九色成人免费视频| 久久久久久久网站| 国产精品最新在线观看| 中文字幕视频一区二区在线有码| 久久久精品视频成人| 欧美精品电影在线| 亚洲午夜未满十八勿入免费观看全集| 日本久久久a级免费| 亚洲精品在线看| 国产性猛交xxxx免费看久久| 国产精品欧美一区二区三区奶水| 久久久中文字幕| 亚洲福利视频久久| 日本久久久久亚洲中字幕| 91高清视频在线免费观看| 亚洲美女免费精品视频在线观看| 97精品一区二区视频在线观看| 92福利视频午夜1000合集在线观看| 成人午夜黄色影院| 精品国产91久久久久久老师| 日韩经典中文字幕| 国产精品国内视频| 亚洲黄色有码视频| 国产日韩在线一区| 久青草国产97香蕉在线视频| 日韩天堂在线视频| 国产中文字幕亚洲| 久久久精品国产网站| 成人综合网网址| 亚洲精品不卡在线| 久久久www成人免费精品张筱雨| 亚洲一区国产精品| 欧美野外wwwxxx| 国产精品久久久久秋霞鲁丝| 日韩高清有码在线| 久久九九精品99国产精品| 韩国一区二区电影| 欧美日韩国产色视频| 欧美日韩国产专区| 激情亚洲一区二区三区四区| 中文字幕一区二区三区电影| 日韩电影在线观看永久视频免费网站| 国产精品狠色婷| 国产欧美日韩综合精品| 亚洲综合av影视| 中文字幕精品www乱入免费视频| 欧美性极品xxxx做受| 亚洲精品久久久久久久久久久久| 国产成人精品日本亚洲| 国产精品久久久久aaaa九色| 色综合导航网站| 欧美巨乳美女视频| 日韩在线免费视频| 日韩中文字幕久久| 亚洲欧美日韩爽爽影院| 91香蕉嫩草影院入口| 日韩美女视频免费看| 91久久国产婷婷一区二区| 久久久国产一区| 成人激情av在线| 欧美精品免费看| 日韩中文字幕在线免费观看| 亚洲欧美另类自拍| 日韩成人在线观看| 91免费看视频.| 久久综合伊人77777| www高清在线视频日韩欧美| 欧美激情视频免费观看| 曰本色欧美视频在线| 一区二区三区高清国产| 亚洲精品久久久久久久久久久| 成人激情在线播放| 性色av一区二区三区在线观看| 欧美日韩国产专区| 亚洲精品美女在线观看播放| 亚洲一区中文字幕| 97精品国产97久久久久久春色| 精品成人在线视频| 国产91成人video| 亚洲色图13p| 日韩网站免费观看高清| 在线日韩精品视频| 国产日韩精品综合网站| 亚洲a级在线播放观看| 97精品国产97久久久久久春色| 亚洲国产精品一区二区久| 一本大道久久加勒比香蕉| 国产精品视频xxx| 亚洲热线99精品视频| 欧美与黑人午夜性猛交久久久| 欧美亚洲成人xxx| 亚洲国产欧美在线成人app| 久久精品国亚洲| 亚洲久久久久久久久久久| 久久99热这里只有精品国产| 国产一区欧美二区三区| 日韩美女主播视频| 大桥未久av一区二区三区| 2019中文字幕全在线观看| 亚洲欧美精品中文字幕在线| 欧美性猛交xxxx乱大交| 国产亚洲激情视频在线| 亚洲最新在线视频| 成人av.网址在线网站| 精品成人久久av| 国产亚洲精品va在线观看| 亚洲精品视频免费在线观看| 国产视频精品一区二区三区| 4438全国亚洲精品在线观看视频| 日本精品久久中文字幕佐佐木| 欧美日韩国产123| 亚洲欧洲国产伦综合| 国产午夜精品久久久| 911国产网站尤物在线观看| 狠狠色狠色综合曰曰| 成人午夜激情免费视频| 亚洲欧洲激情在线| 亚洲欧美日韩天堂一区二区|