国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

javascript轉換靜態圖片,增加粒子動畫效果

2024-05-06 16:20:57
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了javascript轉換靜態圖片,增加粒子動畫效果,非常的炫酷,需要的朋友可以參考下

使用getImageData接口獲取圖片的像素點,然后基于像素點實現動畫效果,封裝成一個簡單的lib

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <title>particle image</title> 
  5. <meta charset="utf-8" /> 
  6. <style> 
  7. #logo { 
  8. margin-left:20px; 
  9. margin-top:20px; 
  10. width:160px; 
  11. height:48px; 
  12. background:url('./images/logo.png'); 
  13. /*border: 1px solid red;*/ 
  14. </style> 
  15. <script type="text/javascript" src="ParticleImage.js"></script> 
  16. <script> 
  17. window.onload = function() { 
  18. ParticleImage.create("logo", "./images/logo.png", "fast"); 
  19. }; 
  20. </script> 
  21. </head> 
  22. <body> 
  23. <div id="logo"></div> 
  24. </body> 
  25. </html> 

ParticleImage.js

 

 
  1. /* 
  2. The MIT License (MIT) 
  3.  
  4. Copyright (c) 2015 arest 
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy of 
  7. this software and associated documentation files (the "Software"), to deal in 
  8. the Software without restriction, including without limitation the rights to 
  9. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 
  10. the Software, and to permit persons to whom the Software is furnished to do so, 
  11. subject to the following conditions: 
  12.  
  13. The above copyright notice and this permission notice shall be included in all 
  14. copies or substantial portions of the Software. 
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
  18. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
  19. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
  20. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
  22. */ 
  23.  
  24. /** 
  25. * Add particle animation for image 
  26. * usage: 
  27. <script type="text/javascript" src="ParticleImage.js"></script> 
  28. <script> 
  29. window.onload = function() { 
  30. // be sure to use image file in your own server (prevent CORS issue) 
  31. ParticleImage.create("logo", "logo_s2.png", "fast"); 
  32. }; 
  33. </script> 
  34. // in html file 
  35. <div id="logo"></div> 
  36. // you can set default background image as usual 
  37. #logo { 
  38. margin-left:20px; 
  39. margin-top:20px; 
  40. width:160px; 
  41. height:48px; 
  42. background:url('logo_s2.png'); 
  43. } 
  44. * 
  45. * @author tianx.qin (rushi_wowen@163.com) 
  46. * @file ParticleImage.js 
  47. * @version 0.9 
  48. */ 
  49. var ParticleImage = (function(window) { 
  50. var container = null, canvas = null
  51. var ctx = null, _spirit = [], timer = null
  52. cw = 0, ch = 0, // container width/height 
  53. iw = 0, ih = 0, // image width/height 
  54. mx = 0, my = 0, // mouse position 
  55. bMove = true
  56. MOVE_SPAN = 4, DEFAULT_ALPHA = 100, 
  57. speed = 100, S = {"fast":10, "mid":100, "low":300}, 
  58. ALPHA = 255 * 255; 
  59.  
  60. // spirit class 
  61. var Spirit = function(data) { 
  62. this.orginal = { 
  63. pos: data.pos, 
  64. x : data.x, y : data.y, 
  65. r : data.r, g : data.g, b : data.b, a : data.a 
  66. }; 
  67. // change state, for animation 
  68. this.current = { 
  69. x : data.x, 
  70. y : data.y, 
  71. a : data.a 
  72. }; 
  73. }; 
  74.  
  75. /** 
  76. * move spirit to original position 
  77. */ 
  78. Spirit.prototype.move = function() { 
  79. var cur = this.current, orig = this.orginal; 
  80. if ((cur.x === orig.x) && (cur.y === orig.y)) { 
  81. //console.log("don't move:" + cur.y); 
  82. return false
  83. //console.log("move:" + cur.y); 
  84. var rand = 1 + Math.round(MOVE_SPAN * Math.random()); 
  85. var offsetX = cur.x - orig.x, 
  86. offsetY = cur.y - orig.y; 
  87. var rad = offsetX == 0 ? 0 : offsetY / offsetX; 
  88. var xSpan = cur.x < orig.x ? rand : cur.x > orig.x ? -rand : 0; 
  89. cur.x += xSpan; 
  90. var tempY = xSpan == 0 ? Math.abs(rand) : Math.abs(Math.round(rad * xSpan)); 
  91. var ySpan = offsetY < 0 ? tempY : offsetY > 0 ? -tempY : 0; 
  92. cur.y += ySpan; 
  93. cur.a = ((cur.x === orig.x) && (cur.y === orig.y)) ? orig.a : DEFAULT_ALPHA; 
  94. return true
  95. }; 
  96.  
  97. /** 
  98. * set random position 
  99. */ 
  100. Spirit.prototype.random = function(width, height) { 
  101. var cur = this.current; 
  102. cur.x = width + Math.round(width * 2 * Math.random()); 
  103. this.current.y = height + Math.round(height * 2 * Math.random()); 
  104. }; 
  105.  
  106. /** 
  107. * set random positions for all spirits 
  108. */ 
  109. var _disorder = function() { 
  110. var len = _spirit.length; 
  111. for (var i = 0; i < len; i++) { 
  112. _spirit[i].random(cw, ch); 
  113. }; 
  114.  
  115. /** 
  116. * start to move spirit 
  117. */ 
  118. var _move = function() { 
  119. var sprt = _spirit; 
  120. var len = sprt.length; 
  121. var isMove = false// whether need to move 
  122. for (var i = 0; i < len; i++) { 
  123. if (sprt[i].move()) { 
  124. isMove = true
  125. isMove ? _redraw() : _stopTimer(); 
  126. }; 
  127.  
  128. /** 
  129. * redraw all spirits while animating 
  130. */ 
  131. var _redraw = function() { 
  132. var imgDataObj = ctx.createImageData(iw, ih); 
  133. var imgData = imgDataObj.data; 
  134. var sprt = _spirit; 
  135. var len = sprt.length; 
  136. //console.log("redraw image : " + len); 
  137. for (var i = 0; i < len; i++) { 
  138. var temp = sprt[i]; 
  139. //console.log("item : " + JSON.stringify(temp)); 
  140. var orig = temp.orginal; 
  141. var cur = temp.current; 
  142. var pos = (cur.y * iw + cur.x) * 4; 
  143. imgData[pos] = orig.r; 
  144. imgData[pos + 1] = orig.g; 
  145. imgData[pos + 2] = orig.b; 
  146. imgData[pos + 3] = cur.a; 
  147. ctx.putImageData(imgDataObj, 0, 0); 
  148. }; 
  149.  
  150. /** 
  151. * add mousemove/mouseclick event 
  152. */ 
  153. var _addMouseEvent = function(c) { 
  154. c.addEventListener("mouseenter"function(e) { 
  155. //console.log("e.y:" + e.clientY + ", " + container.offsetTop); 
  156. _startTimer(); 
  157. }); 
  158. c.addEventListener("click"function() { 
  159. // disorder all spirits and start animation 
  160. _startTimer(); 
  161. }); 
  162. }; 
  163.  
  164. /** 
  165. * calculate all pixels of the logo image 
  166. */ 
  167. var _checkImage = function(imgUrl, callback) { 
  168. // var tempCanvas = document.getElementById("temp"); 
  169. //canvas.width = width; 
  170. //canvas.height = height; 
  171.  
  172. var proc = function(image) { 
  173. var w = image.width, h = image.height; 
  174. iw = w, ih = h; 
  175. //console.log("proc image " + image + "," + w + "," + h); 
  176. canvas = _createCanvas(); 
  177. // hide container background 
  178. container.style.backgroundPosition = (-w) + "px"
  179. container.style.backgroundRepeat = "no-repeat"
  180. ctx.drawImage(image, 0, 0); 
  181. // this may cause security error for CORS issue 
  182. try { 
  183. var imgData = ctx.getImageData(0, 0, w, h); 
  184. var arrData = imgData.data; 
  185. for (var i = 0; i < arrData.length; i += 4) { 
  186. var r = arrData[i], g = arrData[i + 1], b = arrData[i + 2], a = arrData[i + 3]; 
  187. if (r > 0 || g > 0 || b > 0 || a > 0) { 
  188. var pos = i / 4; 
  189. _spirit.push(new Spirit({ 
  190. x : pos % w, y : Math.floor(pos / w),  
  191. r : r, g : g, b : b, a : a 
  192. })); 
  193. return true
  194. catch (e) { 
  195. // do nothing 
  196. return false
  197. //return out; 
  198. }; 
  199.  
  200. var img = new Image(); 
  201. img.src = imgUrl; 
  202. if (img.complete || img.complete === undefined) { 
  203. proc(img) && callback && callback(); 
  204. else { 
  205. img.onload = function() { 
  206. proc(img) && callback && callback(); 
  207. }; 
  208. }; 
  209.  
  210. // use "requestAnimationFrame" to create a timer, need browser support 
  211. var _timer = function(func, dur) { 
  212. //console.log("speed is " + dur); 
  213. var timeLast = null
  214. var bStop = false
  215. var bRunning = false// prevent running more than once 
  216. var _start = function() { 
  217. if (func) { 
  218. if (! timeLast) { 
  219. timeLast = Date.now(); 
  220. func(); 
  221. else { 
  222. var current = Date.now(); 
  223. if (current - timeLast >= dur) { 
  224. timeLast = current; 
  225. func(); 
  226.  
  227. if (bStop) { 
  228. return
  229. requestAnimationFrame(_start); 
  230. }; 
  231.  
  232. var _stop = function() { 
  233. bStop = true
  234. }; 
  235.  
  236. return { 
  237. start : function() { 
  238. if (bRunning) { 
  239. //console.log("already running.."); 
  240. return
  241. //console.log("start running.."); 
  242. bRunning = true
  243. bStop = false
  244. _disorder(); 
  245. _start(); 
  246. }, 
  247. stop : function() { 
  248. _stop(); 
  249. bRunning = false
  250. }; 
  251. }; 
  252.  
  253. var _startTimer = function() { 
  254. if (! timer) { 
  255. timer = _timer(function() { 
  256. bMove && _move(); 
  257. }, speed); 
  258. timer.start(); 
  259. }; 
  260.  
  261. var _stopTimer = function() { 
  262. timer && timer.stop(); 
  263. }; 
  264.  
  265. /** 
  266. * start process 
  267. */ 
  268. var _create = function(imgUrl) { 
  269. _checkImage(imgUrl, function() { 
  270. //_createSpirits(); 
  271. _addMouseEvent(canvas); 
  272. //_startTimer(); 
  273. }); 
  274. }; 
  275.  
  276. var _setSpeed = function(s) { 
  277. S[s] && (speed = S[s]); 
  278. }; 
  279.  
  280. /** 
  281. * check whether browser supports canvas 
  282. */ 
  283. var _support = function() { 
  284. try { 
  285. document.createElement("canvas").getContext("2d"); 
  286. return true
  287. catch (e) { 
  288. return false
  289. }; 
  290.  
  291. /** 
  292. * create a canvas element 
  293. */ 
  294. var _createCanvas = function() { 
  295. var cav = document.createElement("canvas"); 
  296. cav.width = iw; 
  297. cav.height = ih; 
  298. container.appendChild(cav); 
  299. ctx = cav.getContext("2d"); 
  300. return cav; 
  301. }; 
  302.  
  303. /** 
  304. * initialize container params 
  305. */ 
  306. var _init = function(c, s) { 
  307. if ((! c) || (! _support())) { // DIV id doesn't exist 
  308. return false
  309. container = c; 
  310. cw = c.clientWidth; 
  311. ch = c.clientHeight; 
  312. s && _setSpeed(s); 
  313. return true
  314. }; 
  315.  
  316. /** 
  317. * export 
  318. */ 
  319. return { 
  320. "create" : function(cId, imgUrl, s) { // user can set move speed by 's'['fast','mid','low'] 
  321. _init(document.getElementById(cId), s) && _create(imgUrl); 
  322. }; 
  323. })(window); 

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 辉南县| 武山县| 崇文区| 鄂伦春自治旗| 峨边| 祁阳县| 曲阜市| 镇坪县| 宝山区| 平罗县| 公安县| 柳江县| 健康| 连平县| 梅州市| 垫江县| 滕州市| 海南省| 康乐县| 资阳市| 隆回县| 满洲里市| 昆山市| 汕头市| 承德市| 建湖县| 阿拉尔市| 建宁县| 宾阳县| 府谷县| 景洪市| 通海县| 达日县| 泾源县| 唐海县| 华亭县| 密云县| 交口县| 合江县| 彝良县| 加查县|