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

首頁 > 編程 > HTML > 正文

HTML5 Canvas概述

2020-03-24 19:21:35
字體:
來源:轉載
供稿:網友
canvas 是一個新的html' target='_blank'>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 ,唯一不同就是它不含 src 和 alt 屬性。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.它只有兩個屬性,width 和 height,兩個都是可選的,并且都可以 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 html教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产午夜精品视频| 国产美女久久久| 91精品国产综合久久男男| 国产91精品最新在线播放| 992tv成人免费视频| 66m—66摸成人免费视频| 不卡av电影在线观看| 日韩毛片中文字幕| 久久久黄色av| 欧美激情在线有限公司| 秋霞av国产精品一区| 岛国av一区二区三区| 91香蕉嫩草神马影院在线观看| 91高潮精品免费porn| 日本午夜在线亚洲.国产| 欧美制服第一页| 精品国产鲁一鲁一区二区张丽| 在线视频日本亚洲性| 欧美精品九九久久| 国产精品久久久久久亚洲影视| 亚洲欧美国产精品专区久久| 538国产精品视频一区二区| 国内精品久久久久久| 91大神在线播放精品| 一区二区三区 在线观看视| 日韩在线www| 欧美老女人性视频| 欧美一区在线直播| 日韩福利伦理影院免费| 亚洲丝袜在线视频| 久久免费精品日本久久中文字幕| 国内揄拍国内精品少妇国语| 91久久久久久久久久久久久| 91精品久久久久久久久不口人| 91精品在线看| 黄色一区二区三区| 久久久国产一区二区| 欧美影院久久久| 97在线日本国产| 自拍偷拍亚洲区| 在线成人一区二区| 欧美成人午夜影院| 国产精品久久久久久久久免费| 少妇精69xxtheporn| 亚洲国产中文字幕在线观看| 91久久综合亚洲鲁鲁五月天| 亚洲网站在线观看| 26uuu另类亚洲欧美日本老年| 91精品国产九九九久久久亚洲| xvideos亚洲人网站| 欧美激情精品在线| www.xxxx欧美| 国产欧美日韩中文字幕在线| 日韩av综合网站| 97精品国产97久久久久久免费| 亚洲大胆美女视频| 欧美激情videos| 欧美性理论片在线观看片免费| 色与欲影视天天看综合网| 久久99国产综合精品女同| 91久久久久久久久久| 国产成人精品久久二区二区| 日韩在线观看你懂的| 国产成人福利夜色影视| 伦理中文字幕亚洲| 最近的2019中文字幕免费一页| 国产精品第2页| 欧美性黄网官网| 国产精品视频自在线| 亚洲一区中文字幕在线观看| 国产精品海角社区在线观看| 国产精品免费一区豆花| 国产精品入口夜色视频大尺度| 亚洲理论在线a中文字幕| 国产亚洲精品日韩| 欧美日韩国产限制| 精品日韩中文字幕| 大荫蒂欧美视频另类xxxx| 欧美大尺度电影在线观看| 亚洲经典中文字幕| 久久乐国产精品| 国产精品一区二区三区毛片淫片| 伊人久久久久久久久久久久久| 国产精品久久中文| 亚洲人成电影网站色www| 亚洲天堂av在线免费| 秋霞成人午夜鲁丝一区二区三区| 国产精品中文在线| 中文字幕精品www乱入免费视频| 久久中国妇女中文字幕| 中文字幕日本欧美| 色悠悠国产精品| www.亚洲免费视频| 国产欧美久久一区二区| 欧美电影院免费观看| 91免费看片在线| 91精品视频免费看| 欧美一区三区三区高中清蜜桃| 亚洲人成电影网站色www| 日本精品久久中文字幕佐佐木| 成人网页在线免费观看| 日韩在线观看免费网站| 欧美日韩加勒比精品一区| 久久天天躁夜夜躁狠狠躁2022| 亚洲精品综合久久中文字幕| 中文字幕日韩免费视频| 精品成人69xx.xyz| 成人黄色大片在线免费观看| 成人做爰www免费看视频网站| 国产精品www网站| 国产精品久久久久久久久久久久| 精品美女永久免费视频| 亚洲人成电影网站色www| 欧美一级大片在线免费观看| 欧美大片欧美激情性色a∨久久| 97avcom| 2019中文字幕在线免费观看| 精品色蜜蜜精品视频在线观看| 国产在线视频91| 久久久久久国产免费| 欧美激情一区二区三区久久久| 国产欧美日韩综合精品| 亚洲自拍欧美另类| 国产精品99久久久久久人| 欧美精品国产精品日韩精品| 成人激情视频免费在线| 久久高清视频免费| yw.139尤物在线精品视频| 日本不卡视频在线播放| 国产日韩av在线播放| 日本人成精品视频在线| 久久激情五月丁香伊人| 国产精品主播视频| 欧美激情国产精品| 色阁综合伊人av| 久久久久久亚洲| 亚洲欧美色图片| 国产精品白丝av嫩草影院| 欧美中文字幕在线观看| 久久99精品国产99久久6尤物| 久久伊人91精品综合网站| 国产成人在线一区二区| 欧美日本高清一区| 91在线网站视频| 国产精品久久视频| 亚洲一区二区三区在线视频| 国产成人+综合亚洲+天堂| 久热精品视频在线观看| 亚洲最大福利视频网| 日韩有码在线观看| 亚洲一区二区日本| 日韩免费看的电影电视剧大全| 久久久亚洲成人| 亚洲bt欧美bt日本bt| 欧美麻豆久久久久久中文| 国语自产精品视频在线看抢先版图片| 欧美精品在线观看| 亚洲美女精品成人在线视频| 丝袜亚洲另类欧美重口| 日韩av中文字幕在线播放| 成人性生交大片免费看小说| 亚洲欧洲在线视频| 91沈先生作品| 国产91热爆ts人妖在线|