我已經使用畫布標記完成了一點全屏幕背景疊加,但已經擊中了性能牆。我所做的是創建一個名爲#OverlayPic的容器,並將其設置爲100%x 100%,顯示:無。在這個容器裏面是我的畫布元素。畫布圖像像素操作優化
當啓動時,jQuery將圖像加載到畫布上並獲取像素信息作爲數組。 switch語句接受用戶爲其期望的過濾器效果設置的選項。代碼都可以工作,但速度非常慢(我認爲這主要是由於我的結構如何,但我不確定更好的方法)。
updateCanvas:function(onResize){
var img=new Image(); img.src=Main.ConSRC,
img.onload=function(){
var canvas=document.getElementById('Box_Canvas'),
ctx=canvas.getContext("2d"),
winW=$(window).width(), winH=$(window).height(),
imgW=this.width, imgH=this.height, smallestSide=Math.min(imgW,imgH);
// SETUP IMAGE PROPORTIONS
switch(smallestSide){
case imgW:
var width=winW,height=width*(imgW/imgH);
if(height < winH){ var height=winH, width=height*(imgW/imgH); };
break;
case imgH:
var height=winH,width=height*(imgW/imgH);
if(width < winW){ var width=winW, height=width*(imgH/imgW); };
break;
};
// DRAW IMAGE ON THE CANVAS
ctx.clearRect(0, 0, width*1.3, height*1.3);
ctx.drawImage(img,0,0,width*1.3,height*1.3);
// IMAGE FILTERS
var imgdata=ctx.getImageData(0, 0, width, height), pix=imgdata.data, l=pix.length;
switch($this.data.bg_pic_filter){
// all filter code cases are here...
};
// APPLY THE FILER
ctx.putImageData(imgdata, 0, 0);
// FADE IN OVERLAY
if(!onResize){
Main.OBJ.$OverlayPic.fadeTo($this.data.bg_pic_speed, $this.data.bg_pic_opacity);
};
};
},
該函數在2個地方被調用。
當用戶單擊指定的元素時,疊加層將淡入,並且畫布將加載已過濾的圖像。
在窗口調整大小事件(onResize arg)上,爲了維護應用的過濾器,否則它只是默認回到原始圖像?
有沒有人有任何優化建議?謝謝!