首頁  >  文章  >  web前端  >  IE9下html5初試小刀 _html5教學技巧

IE9下html5初試小刀 _html5教學技巧

WBOY
WBOY原創
2016-05-16 15:51:151785瀏覽

mvc是個好東西,為什麼一入行的時候不去學一下,非要等到asp.net mvc出來了才去學;orm是個好東西,幹嘛非要等到EF出來了才去學;html5是個好東西,幹嘛非要等到IE9出來了才去學? ……

——我想自己應該改掉這個壞毛病。

廢話不多說了。

需求:模仿dreamweaver裡為圖片畫上錨點的功能,產生html程式碼裡的coords值的功能。

技術分析:直覺告訴我,html5 canvas可以勝任。

由於從來沒實質接觸過canvas,只看過別人用canvas開發的demo,只好bing一下html5 canvas的教程咯。發現了下面的連結:http://kb.operachina.com/node/190

看完文件寫程式碼:

程式碼分析:

1.1 html:要用一個圖片作底,canvas放在它上面以供畫圖

1.2 css:你起碼要位置放對、該透明的地方透明

1.3 javascript:鼠標事件要響應仨:mousedown,mousemove,mouseup


複製代碼
代碼如下:


IE9下html5初試小刀 _html5教學技巧

some info to tell the people whose broswer doesn't support html5





有經驗的同學可能一看這html5程式碼就知道這注定是個悲劇,當有img元素在canvas下面時,不管怎樣canvas就是不透明,忘了canvas上可不可以畫上東西了,應該也是不行的。看來這canvas元素有“潔癖”,不願和其他低級元素同流合污。就算我要退而求其次,作為cantainer的背景元素出現都不行。我的感覺是這個canvas可能不會對其他元素透明的。所以上面的程式碼其實是錯誤的程式碼...

那怎麼樣才能實現類似photoshop裡圖層的效果呢?那就是多弄幾個canvas元素,把上面的img換成canvas,然後把img繪製到這個canvas上,這樣canvas對canvas就是透明的了。哎...代碼如下:

複製代碼
代碼如下:




some info to tell the people whose broswer doesn't support html5





好了html算搞定了,接下去就是往canvas上繪圖,借助於javascript,這個任務非常簡單。


複製程式碼
程式碼如下:
. ', function () {
// Get the canvas element.
var elem = document.getElementById('bg');
if (!elem || !elem.getContext) {
return ;
}
// Get the canvas 2d context.
var context = elem.getContext('2d');
if (!context || !context.drawImage) {
return ;
}
// Create a new image.
var img = new Image();
// Once it's loaded draw the image on the canvas.
img.addEventListener('load ', function () {
// Original resolution: x, y.
context.drawImage(this, 0, 0);
// Now resize the image: x, y, w, h.
context.drawImage(this, 160, 0, 120, 70);
// Crop and resize the image: sx, sy, sw, sh, dx, dy, dw, dh.
context. drawImage(this, 8, 20, 140, 50, 0, 150, 350, 70);
}, false);
img.src = 'http://www.sh1800.net/NavPic/20100917 .jpg';
}, false);
//直接在文檔裡拿下來的程式碼請注意為了opera和ie9 onload事件是必須要的,不然圖片會是一片空白,當然Chrome下不會這樣

未完待續....
原文網址http://www.cnblogs.com/ice6/archive/2010/09/18/1830020.html
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn