十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
HTML5中drawImage使用時(shí)遇到的問題及解決方法

10余年的云浮網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營(yíng)銷網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整云浮建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“云浮網(wǎng)站設(shè)計(jì)”,“云浮網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
HTML5中drawImage使用時(shí)遇到的問題及解決方法
使用Image遇到的問題:
$(function() {
var jsCanv = document.getElementById("canv");
var oCanv = jsCanv.getContext("2d");
var img = new Image();
img.src = "img.png";
oCanv.drawImage(img, 220, 30);
})
瀏覽器不支持
其實(shí)這種寫法是有錯(cuò)誤的,實(shí)際上只要一刷新圖片就不顯示出來。要想保證刷新正常顯示需要在Image onload的`時(shí)候重繪一次才行。測(cè)試在chrome 19下會(huì)出現(xiàn)的問題。
解決方案
$(function() {
var jsCanv = document.getElementById("canv");
var oCanv = jsCanv.getContext("2d");
var img = new Image();
img.src = "img.png";
img.onload = function() {
oCanv.drawImage(img, 220, 30);
}
})
瀏覽器不支持
希望本文所述對(duì)大家的html5程序設(shè)計(jì)有所幫助。
;
可以參考JavaScript Image Preloader 。
我寫了一個(gè)代碼,你看一下:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
title無標(biāo)題文檔/title
/head
body onload=preloader()
canvas id="pad" width="100%" height="100%"/canvas
script language="JavaScript"
var ctx=document.getElementById('pad').getContext('2d');
var imageObj=new Image();
preloader();
imageObj.onload=function(){
for(var i=0;i6;i++)
ctx.drawImage(imageObj,0+i*20,0+1*20,80,40);
}
function preloader()
{
// counter
var i = 0;
// create object
//imageObj = new Image();
// set image list
images = new Array();
images[0]="images/0.jpg"
images[1]="images/1.jpg"
images[2]="images/2.jpg"
images[3]="images/3.jpg"
images[4]="images/4.jpg"
images[5]="images/5.jpg"
// start preloading
for(i=0; i=3; i++)
{
imageObj.src=images[i];
}
}
/script
/body
/html
主要思想:
首先要準(zhǔn)備一張有連續(xù)幀的圖片,然后利用HTML5 Canvas的draw方法在不同的時(shí)間間隔繪制不同的幀,這樣看起來就像動(dòng)畫在播放。
關(guān)鍵技術(shù)點(diǎn):
JavaScript 函數(shù)setTimeout()有兩個(gè)參數(shù),第一個(gè)是參數(shù)可以傳遞一個(gè)JavaScript方法,
另外一個(gè)參數(shù)代表間隔時(shí)間,單位為毫秒數(shù)。代碼示例:
setTimeout( update, 1000/30);
Canvas的API-drawImage()方法,需要指定全部9個(gè)參數(shù):
ctx.drawImage(myImage, offw, offh, width,height, x2, y2, width, height);
其中offw, offh是指源圖像的起始坐標(biāo)點(diǎn),width, height表示源圖像的寬與高,x2,y2表
示源圖像在目標(biāo)Canvas上的起始坐標(biāo)點(diǎn)。
!DOCTYPE html
html
head
meta http-equiv="X-UA-Compatible" content="chrome=IE8"
meta http-equiv="Content-type" content="text/html;charset=UTF-8"
titleCanvas Mouse Event Demo/title
link href="default.css" rel="stylesheet" /
script
var ctx = null; // global variable 2d context
var started = false;
var mText_canvas = null;
var x = 0, y =0;
var frame = 0; // 22 5*5 + 2
var imageReady = false;
var myImage = null;
var px = 300;
var py = 300;
var x2 = 300;
var y2 = 0;
window.onload = function() {
var canvas = document.getElementById("animation_canvas");
console.log(canvas.parentNode.clientWidth);
canvas.width = canvas.parentNode.clientWidth;
canvas.height = canvas.parentNode.clientHeight;
if (!canvas.getContext) {
console.log("Canvas not supported. Please install a HTML5 compatible browser.");
return;
}
// get 2D context of canvas and draw rectangel
ctx = canvas.getContext("2d");
ctx.fillStyle="black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
myImage = document.createElement('img');
myImage.src = "../robin.png";
myImage.onload = loaded();
}
function loaded() {
imageReady = true;
setTimeout( update, 1000/30);
}
function redraw() {
ctx.clearRect(0, 0, 460, 460)
ctx.fillStyle="black";
ctx.fillRect(0, 0, 460, 460);
// find the index of frames in image
var height = myImage.naturalHeight/5;
var width = myImage.naturalWidth/5;
var row = Math.floor(frame / 5);
var col = frame - row * 5;
var offw = col * width;
var offh = row * height;
// first robin
px = px - 5;
py = py - 5;
if(px -50) {
px = 300;
}
if(py -50) {
py = 300;
}
//var rate = (frame+1) /22;
//var rw = Math.floor(rate * width);
//var rh = Math.floor(rate * height);
ctx.drawImage(myImage, offw, offh, width, height, px, py, width, height);
// second robin
x2 = x2 - 5;
y2 = y2 + 5;
if(x2 -50) {
x2 = 300;
y2 = 0;
}
ctx.drawImage(myImage, offw, offh, width, height, x2, y2, width, height);
}
function update() {
redraw();
frame++;
if (frame = 22) frame = 0;
setTimeout( update, 1000/30);
}
/script
/head
body
h1HTML Canvas Animations Demo - By Gloomy Fish/h1
prePlay Animations/pre
div id="my_painter"
canvas id="animation_canvas"/canvas
/div
/body
/html
使用HTML5 Canvas動(dòng)態(tài)的繪制拓?fù)鋱D:
HTML5中引入新的元素canvas,其drawImage 方法允許在 canvas 中插入其他圖像( img 和 canvas 元素) 。drawImage函數(shù)有三種函數(shù)原型:
語法:
drawImage(image, dx, dy)
drawImage(image, dx, dy, dw, dh)
drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)
*第一個(gè)參數(shù)image可以用HTMLImageElement,HTMLCanvasElement或者HTMLVideoElement作為參數(shù)。
*dx、dy是image在canvas中定位的坐標(biāo)值;
*dw、dh是image在canvas中即將繪制區(qū)域(相對(duì)dx和dy坐標(biāo)的偏移量)的寬度和高度值;
*sx、sy是image所要繪制的起始位置;
*sw、sh是image所要繪制區(qū)域(相對(duì)image的sx和sy坐標(biāo)的偏移量)的寬度和高度值。
舉例:
!DOCTYPE html
html
body
p要使用的圖像:/p
img id="tulip" src="/i/eg_tulip.jpg" alt="The Tulip" /
p畫布:/p
canvas id="myCanvas" width="500" height="300" style="border:1px solid #d3d3d3;background:#ffffff;"
Your browser does not support the HTML5 canvas tag.
/canvas
script
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var img=document.getElementById("tulip");
ctx.drawImage(img,10,10);
/script
/body
/html
通常不會(huì). 但是在加載image的時(shí)候最好用onload事件判斷圖片是否加載完成. 然后再getImageData或者putImageData:
var canvas = document.getElementById('canvas');
canvas.width = 200;
canvas.height = 200;
var ctx = canvas.getContext('2d');
var imgDat;
var img = new Image();
img.onload = function() {
ctx.drawImage(img, 0, 0);
imgDat = ctx.getImageData(0, 0, canvas.width, canvas.height);
console.log(imgDat); // Output imageData;
}
img.src = 'comment.gif';
主要思想:
首先要準(zhǔn)備一張有連續(xù)幀的圖片,然后利用HTML5
Canvas的draw方法在不同的時(shí)間間隔繪制不同的幀,這樣看起來就像動(dòng)畫在播放。
關(guān)鍵技術(shù)點(diǎn):
JavaScript
函數(shù)setTimeout()有兩個(gè)參數(shù),第一個(gè)是參數(shù)可以傳遞一個(gè)JavaScript方法,
另外一個(gè)參數(shù)代表間隔時(shí)間,單位為毫秒數(shù)。代碼示例:
setTimeout(
update,
1000/30);
Canvas的API-drawImage()方法,需要指定全部9個(gè)參數(shù):
ctx.drawImage(myImage,
offw,
offh,
width,height,
x2,
y2,
width,
height);
其中offw,
offh是指源圖像的起始坐標(biāo)點(diǎn),width,
height表示源圖像的寬與高,x2,y2表
示源圖像在目標(biāo)Canvas上的起始坐標(biāo)點(diǎn)。
!DOCTYPE
html
html
head
meta
http-equiv="X-UA-Compatible"
content="chrome=IE8"
meta
http-equiv="Content-type"
content="text/html;charset=UTF-8"
titleCanvas
Mouse
Event
Demo/title
link
href="default.css"
rel="stylesheet"
/
script
var
ctx
=
null;
//
global
variable
2d
context
var
started
=
false;
var
mText_canvas
=
null;
var
x
=
0,
y
=0;
var
frame
=
0;
//
22
5*5
+
2
var
imageReady
=
false;
var
myImage
=
null;
var
px
=
300;
var
py
=
300;
var
x2
=
300;
var
y2
=
0;
window.onload
=
function()
{
var
canvas
=
document.getElementById("animation_canvas");
console.log(canvas.parentNode.clientWidth);
canvas.width
=
canvas.parentNode.clientWidth;
canvas.height
=
canvas.parentNode.clientHeight;
if
(!canvas.getContext)
{
console.log("Canvas
not
supported.
Please
install
a
HTML5
compatible
browser.");
return;
}
//
get
2D
context
of
canvas
and
draw
rectangel
ctx
=
canvas.getContext("2d");
ctx.fillStyle="black";
ctx.fillRect(0,
0,
canvas.width,
canvas.height);
myImage
=
document.createElement('img');
myImage.src
=
"../robin.png";
myImage.onload
=
loaded();
}
function
loaded()
{
imageReady
=
true;
setTimeout(
update,
1000/30);
}
function
redraw()
{
ctx.clearRect(0,
0,
460,
460)
ctx.fillStyle="black";
ctx.fillRect(0,
0,
460,
460);
//
find
the
index
of
frames
in
image
var
height
=
myImage.naturalHeight/5;
var
width
=
myImage.naturalWidth/5;
var
row
=
Math.floor(frame
/
5);
var
col
=
frame
-
row
*
5;
var
offw
=
col
*
width;
var
offh
=
row
*
height;
//
first
robin
px
=
px
-
5;
py
=
py
-
5;
if(px
-50)
{
px
=
300;
}
if(py
-50)
{
py
=
300;
}
//var
rate
=
(frame+1)
/22;
//var
rw
=
Math.floor(rate
*
width);
//var
rh
=
Math.floor(rate
*
height);
ctx.drawImage(myImage,
offw,
offh,
width,
height,
px,
py,
width,
height);
//
second
robin
x2
=
x2
-
5;
y2
=
y2
+
5;
if(x2
-50)
{
x2
=
300;
y2
=
0;
}
ctx.drawImage(myImage,
offw,
offh,
width,
height,
x2,
y2,
width,
height);
}
function
update()
{
redraw();
frame++;
if
(frame
=
22)
frame
=
0;
setTimeout(
update,
1000/30);
}
/script
/head
body
h1HTML
Canvas
Animations
Demo
-
By
Gloomy
Fish/h1
prePlay
Animations/pre
div
id="my_painter"
canvas
id="animation_canvas"/canvas
/div
/body
/html