Home Web Front-end H5 Tutorial HTML5高级编程之像素处理及粒子效果

HTML5高级编程之像素处理及粒子效果

May 17, 2016 am 09:08 AM

       HTML5中的像素处理,需要用到getImageData和putImageData两个函数,先用getImageData复制canvas画布中的像素数据,然后对获取的像素数据进行处理,最后再通过putImageData将处理完的数据粘贴到canvas画布。我们不妨把中间处理像素的过程称作像素的批处理,由于像素的复制和粘贴是两个比较费时的过程,为了更高效的对像素进行处理,我们应该在一次批处理过程中尽可能处理更多的像素数据,来减少像素的复制和粘贴这两个操作。

        这听起来似乎是一个相当麻烦的过程,可能有些朋友光是看到上面的几句话估计就已经开始不耐烦了,其实我也是这么认为的,所以我把这一麻烦的过程封装到了lufylegend引擎中的LBitmapData类中,你可以像处理flash的像素一样处理HTML5中的canvas,这听起来是不是很有趣?如果你有兴趣,那么请跟着我一起来看一下这个好玩儿的小功能。

        首先,来认识一下LBitmapData,它通常是用来保存Image对象的,具体用法我就不多说了,大家可以看一下API文档。这里我主要介绍一下如何用它来批量处理像素。

        下面是LBitmapData中的两个函数
函数 功能
getPixel(x,y,colorType) 返回一个表示 BitmapData 对象中在某个特定点 (x, y) 处的 RGB 像素值的数组。其中colorType为需要获取的像素数据的格式,默认为像素数组,当设置成字符串"number"的时候,返回number型的像素
setPixel(x,y,data) 设置 LBitmapData 对象的单个像素。其中data为像素值(支持像素数组,#FF0000,0xFF000等三种格式)

上面这两个函数是获取和设置单个像素,当我们需要一次性获取或设置一个区域的像素的时候,对应的两个函数如下

函数 功能
getPixels(rect) 返回一组表示 BitmapData 对象中在某个特定区域的 RGB 像素值的数组。其中rect为LRectangle对象,是一个矩形。
setPixels(rect, data) 将像素数据数组转换粘贴到指定的矩形区域。其中data为像素值(支持像素数组,#FF0000,0xFF000等三种格式)

我们先来准备一张图片,比如下面鄙人这张帅帅的头像。

HTML5高级编程之像素处理及粒子效果

  1. bitmapData = new LBitmapData(imglist["face"]);  
  2. bitmapData.lock();  
  3. var img = bitmapData.getPixels(new LRectangle(75,50,100,100));  

        有时候我们需要对LBitmapData进行多次像素的复制和粘贴操作,这个时候可以使用lock函数,它可以将像素数组缓存起来,在这个过程中,所做的所有的像素操作都是对这个缓存数组进行操作,当操作完毕后,调用相应的unlock函数,将操作完的像素数据粘贴回LBitmapData中。

        比如下面这样,我们将复制完的数组中的一部分,分四次粘贴到另外一个LBitmapData的四个不同的位置上。

  1. bitmapData2 = new LBitmapData(null,0,0,500,400,LBitmapData.DATA_CANVAS);  
  2. bitmapData2.lock();  
  3. bitmapData2.setPixels(new LRectangle(50,30,50,50),img);  
  4. bitmapData2.setPixels(new LRectangle(100,30,50,50),img);  
  5. bitmapData2.setPixels(new LRectangle(150,30,50,50),img);  
  6. bitmapData2.setPixels(new LRectangle(200,30,50,50),img);  
  7. bitmapData2.unlock();  

       上面的代码,首先创建了一个空的LBitrmapData对象,最后一个参数是lufylegend-1.8.8版中的新功能,将LBitrmapData对象中保存的数据转换为canvas对象,这样可以提高像素操作的效率。

        执行代码效果如下

HTML5高级编程之像素处理及粒子效果

       当然,你也可以对这些像素做一些处理,比如下面这样

  1. bitmapData2.lock();  
  2. var rect = new LRectangle(50,100,100,100);  
  3. var rect1 = new LRectangle(150,100,100,100);  
  4. for(var y=0;y;y++){  
  5.     for(var x=0;x;x++){  
  6.         var i = y*4*100+x*4;  
  7.         bitmapData2.setPixel(rect.x+rect.width-x,y+rect.y,[img.data[i],img.data[i+1],img.data[i+2],img.data[i+3]]);  
  8.     }  
  9. }  
  10. for(var y=0;y;y++){  
  11.     for(var x=0;x;x++){  
  12.         var i = y*4*100+x*4;  
  13.         bitmapData2.setPixel(x+rect1.x,y+rect1.y,[img.data[i],img.data[i+1],img.data[i+2],img.data[i+3]]);  
  14.     }  
  15. }  
  16. bitmapData2.unlock();  


       效果如下

HTML5高级编程之像素处理及粒子效果

      可以看到,我们成功的通过处理像素,将图片翻转了过来。

       当然图片翻转不需要这么麻烦,在lufylegend.js引擎中,你只需要将对象的属性scaleX设置为-1就可以实现图片的翻转。这里我们主要是为了说明像素的处理很灵活而已。

       好了,有了上面的介绍,我们可以用这些API来制作一个酷酷的粒子效果,效果如下。

HTML5高级编程之像素处理及粒子效果

首先,我们先在画布上加一个文本

  1. var labelText = new LTextField();  
  2. labelText.color = "#000000";  
  3. labelText.weight = "bolder";  
  4. labelText.text = getParameter("k");  
  5. if(!labelText.text)labelText.text="lufylegend.js";  
  6. labelText.size = 50;  
  7. var w = labelText.getWidth()*1.1;  
  8. var h = labelText.size*1.5;  
  9. labelText.width = w;  
  10. labelText.setWordWrap(true,h);  
  11. labelText.x = (LGlobal.width - w)*0.5;  
  12. labelText.y = (LGlobal.height - h)*0.5;  
  13.   
  14. backLayer = new LSprite();  
  15. addChild(backLayer);  
  16. backLayer.addChild(labelText);  


效果如下

HTML5高级编程之像素处理及粒子效果

上面唯一需要解释的是下面几行

  1. var w = labelText.getWidth()*1.1;  
  2. var h = labelText.size*1.5;  
  3. labelText.width = w;  
  4. labelText.setWordWrap(true,h);  

       其实只需要用getWidth()和getHeight()就能获取文本的高和宽,但是因为canvas中没有获取文本高度的函数,所以引擎中用了一个不太准确的方式来获取(当然,这一点在引擎下次更新中会得到完美的解决),本次开发,所使用的文本高度和宽度都必须不小于文本的原始大小,所以,我给文本重新设定了一下略大的高度和宽度。
      接下来,我们利用LBitmapData对象的draw函数,把这个文本转换为LBitmapData对象,为什么要转换成LBitmapData对象?请接着往下看,一会儿就知道了。


  1. bitmapData = new LBitmapData("#000000",0,0,LGlobal.width,LGlobal.height,LBitmapData.DATA_CANVAS);  
  2. bitmapData.draw(backLayer);  

      上面的处理其实都是铺垫,不是用来真正显示的,下面再来创建一个LBitmapData空对象,并通过LBitmap将其绘制到canvas画布上。

  1. snowBack = new LBitmapData(null,0,0,LGlobal.width,LGlobal.height,LBitmapData.DATA_CANVAS);  
  2. var bitmap = new LBitmap(snowBack);  
  3. backLayer.addChild(bitmap);

重点来了,我现在需要对snowBack对象的像素不断的进行处理,这个简单,我们通过ENTER_FRAME来实现

backLayer.addEventListener(LEvent.ENTER_FRAME,run);

       上面的效果图,可以看到,我需要往画布上不断的添加白色的粒子,就和下雪一样。但是这些白色粒子,不能直接画到画布上,我们需要先将这些粒子添加到一个缓存数组中,然后来批量的操作它们,下面的函数用来生成一个粒子,参数分别是(x坐标,y坐标,下降加速度,x轴方向速度,y轴方向速度)。

  1. function particle(px,py,ps,pvx,pvy){  
  2.     if(typeof ps == UNDEFINED)ps = 1;  
  3.     if(typeof pvx == UNDEFINED)pvx = 0;  
  4.     if(typeof pvy == UNDEFINED)pvy = 0;  
  5.     _snow.push({x:px,y:py,s:ps,vx:pvx,vy:pvy});  
  6. }  

       我们通过不断的调用这个函数,来向画布中不断的添加白色粒子,添加到画布上的粒子,会做向下类似于自由落体的加速运动,在运动过程中会遇到阻碍,就是前面在画面上显示过的文字,当粒子碰到文字的时候,受到阻力,运动变得缓慢,这样粒子在有文字的地方不断的受到文字的阻碍,大量的白色粒子就会聚集到文字处,就形成了上面的粒子效果。

      下面的函数用来检验指定坐标处是否处在文字上

  1. function checkPixel(x,y){  
  2.     var pixel = bitmapData.getPixel(x,y);  
  3.     for(var i=0;i;i++){  
  4.         if(pixel[i])return true;  
  5.     }  
  6.     return false;  
  7. }  

      原理就是获取坐标点像素,然后检验像素点的颜色,现在知道为什么要把前面的文字转换成LBitmapData对象了吧,就是为了要获取指定点的像素值。

      

12下一页

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

What exactly does H5 page production mean? What exactly does H5 page production mean? Apr 06, 2025 am 07:18 AM

H5 page production refers to the creation of cross-platform compatible web pages using technologies such as HTML5, CSS3 and JavaScript. Its core lies in the browser's parsing code, rendering structure, style and interactive functions. Common technologies include animation effects, responsive design, and data interaction. To avoid errors, developers should be debugged; performance optimization and best practices include image format optimization, request reduction and code specifications, etc. to improve loading speed and code quality.

How do I handle user location privacy and permissions with the Geolocation API? How do I handle user location privacy and permissions with the Geolocation API? Mar 18, 2025 pm 02:16 PM

The article discusses managing user location privacy and permissions using the Geolocation API, emphasizing best practices for requesting permissions, ensuring data security, and complying with privacy laws.

How to make h5 click icon How to make h5 click icon Apr 06, 2025 pm 12:15 PM

The steps to create an H5 click icon include: preparing a square source image in the image editing software. Add interactivity in the H5 editor and set the click event. Create a hotspot that covers the entire icon. Set the action of click events, such as jumping to the page or triggering animation. Export H5 documents as HTML, CSS, and JavaScript files. Deploy the exported files to a website or other platform.

What application scenarios are suitable for H5 page production What application scenarios are suitable for H5 page production Apr 05, 2025 pm 11:36 PM

H5 (HTML5) is suitable for lightweight applications, such as marketing campaign pages, product display pages and corporate promotion micro-websites. Its advantages lie in cross-platformity and rich interactivity, but its limitations lie in complex interactions and animations, local resource access and offline capabilities.

html next page function html next page function Apr 06, 2025 am 11:45 AM

<p>The next page function can be created through HTML. The steps include: creating container elements, splitting content, adding navigation links, hiding other pages, and adding scripts. This feature allows users to browse segmented content, displaying only one page at a time, and is suitable for displaying large amounts of data or content. </p>

How do I use the HTML5 Drag and Drop API for interactive user interfaces? How do I use the HTML5 Drag and Drop API for interactive user interfaces? Mar 18, 2025 pm 02:17 PM

The article explains how to use the HTML5 Drag and Drop API to create interactive user interfaces, detailing steps to make elements draggable, handle key events, and enhance user experience with custom feedback. It also discusses common pitfalls to a

Does H5 page production require continuous maintenance? Does H5 page production require continuous maintenance? Apr 05, 2025 pm 11:27 PM

The H5 page needs to be maintained continuously, because of factors such as code vulnerabilities, browser compatibility, performance optimization, security updates and user experience improvements. Effective maintenance methods include establishing a complete testing system, using version control tools, regularly monitoring page performance, collecting user feedback and formulating maintenance plans.

See all articles