PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/js/slice.js

https://github.com/bobwol/BlocksJS
JavaScript | 490 lines | 345 code | 93 blank | 52 comment | 105 complexity | e2699e790122e37cdbfb178e49c4978a MD5 | raw file
  1. /*
  2. Copyright (c) 2013 William Malone (www.williammalone.com)
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  4. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  5. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  6. */
  7. /*global window, document, Image, Float32Array */
  8. var BLOCKS;
  9. if (BLOCKS === undefined) {
  10. BLOCKS = {};
  11. }
  12. BLOCKS.slice = function (options) {
  13. "use strict";
  14. var slice = BLOCKS.view(options),
  15. // Properties
  16. imageResource, frameWidth, frameHeight, paused, texture, tmpCtx, cropWidth, cropHeight, frameOffsetX, frameOffsetY, mirrorX, mirrorY,
  17. drawBounds = false,
  18. frameCnt = 0,
  19. loopIndex = 0,
  20. curFrameIndex = 0,
  21. // Private Methods
  22. onResourceLoaded = function () {
  23. // Will be used for webGL enabled contexts
  24. //if (slice.layer && slice.layer.webGLEnabled) {
  25. // prepareWebGLContext(slice.layer.ctx);
  26. //}
  27. // Set the sprite dimensions to the image dimensions
  28. // Note: Divide the width by the number of frames in the sprite sheet if an animation. If the sprite is only an image then the number of frames will be 1.
  29. if (imageResource) {
  30. frameWidth = imageResource.image.width / slice.numberOfFrames;
  31. frameHeight = imageResource.image.height;
  32. slice.width = imageResource.image.width / slice.numberOfFrames;
  33. slice.height = imageResource.image.height;
  34. }
  35. };
  36. slice.loop = options && options.loop;
  37. slice.frameDelay = (options && options.frameDelay !== undefined) ? options.frameDelay : 4;
  38. slice.numberOfFrames = (options && options.numberOfFrames) || 1;
  39. slice.autoPlay = (options && options.autoPlay !== undefined) ? options.autoPlay : true;
  40. slice.resetOnComplete = (options && options.resetOnComplete !== undefined) ? options.resetOnComplete : true;
  41. // Public Methods
  42. slice.update = function () {
  43. if (!paused) {
  44. // If the slice has an image associated with it
  45. if (imageResource) {
  46. // If the sprite is an animation
  47. if (slice.numberOfFrames > 1) {
  48. frameCnt += 1;
  49. // If the current frame is the last frame
  50. if (curFrameIndex >= slice.numberOfFrames - 1) {
  51. if (frameCnt >= slice.frameDelay) {
  52. frameCnt = 0;
  53. loopIndex += 1;
  54. if (slice.loop === true || (typeof slice.loop === "number" && loopIndex < slice.loop)) {
  55. // Reset the frame back to the first frame
  56. curFrameIndex = 0;
  57. slice.dirty = true;
  58. } else {
  59. if (slice.resetOnComplete) {
  60. // Reset the frame back to the first frame
  61. curFrameIndex = 0;
  62. slice.dirty = true;
  63. }
  64. paused = true;
  65. (function () {
  66. var callback;
  67. if (slice.callback) {
  68. // Save the callback in case the slice is destroyed after the complete event
  69. callback = slice.callback;
  70. slice.callback = null;
  71. }
  72. // Dispatch the complete event before any callback
  73. slice.dispatchEvent("complete");
  74. // If there is a callback then invoke it now
  75. if (callback) {
  76. callback();
  77. }
  78. }());
  79. }
  80. }
  81. } else {
  82. if (frameCnt >= slice.frameDelay) {
  83. // Go to the next frame
  84. curFrameIndex += 1;
  85. frameCnt = 0;
  86. slice.dirty = true;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. // Go to the next frame in the animation
  93. };
  94. slice.pause = function () {
  95. paused = true;
  96. };
  97. slice.unpause = function () {
  98. paused = false;
  99. };
  100. slice.reset = function () {
  101. slice.callback = null;
  102. if (frameCnt !== 0 || curFrameIndex !== 0 || loopIndex !== 0) {
  103. slice.dirty = true;
  104. }
  105. frameCnt = 0;
  106. curFrameIndex = 0;
  107. loopIndex = 0;
  108. };
  109. slice.stop = function () {
  110. paused = true;
  111. slice.reset();
  112. };
  113. slice.play = function (callback) {
  114. // If on the last frame then start over
  115. if (curFrameIndex >= slice.numberOfFrames - 1) {
  116. slice.stop();
  117. }
  118. paused = false;
  119. // Assign an optional callback to be played once the animation is complete
  120. slice.callback = callback;
  121. };
  122. slice.render = function (e) {
  123. var i, bounds, restoreNeeded, context, cameraOffset, x, y;
  124. // Prevent alpha from being negative
  125. if (slice.alpha < 0) {
  126. slice.alpha = 0;
  127. } else if (slice.alpha > 1) {
  128. slice.alpha = 1;
  129. }
  130. if (slice.dirty && slice.visible && slice.alpha !== 0 && slice.cropWidth !== 0 && slice.cropHeight !== 0) {
  131. cameraOffset = {
  132. x: (e && e.camera && e.camera.x) || 0,
  133. y: (e && e.camera && e.camera.y) || 0
  134. };
  135. // Set local x and y to increases performance when slice is associated to a stack
  136. x = slice.worldX;
  137. y = slice.worldY;
  138. // If the slice has an image associated with it
  139. if (imageResource) {
  140. if (slice.layer) {
  141. context = slice.layer.ctx;
  142. // Using webGL
  143. if (slice.layer.webGLEnabled) {
  144. //context.bindTexture(context.TEXTURE_2D, texture);
  145. //setBufferData(
  146. // x,
  147. // y,
  148. // slice.cropWidth || slice.width,
  149. // slice.cropHeight || slice.height);
  150. //context.drawArrays(context.TRIANGLES, 0, 6);
  151. //context.bindTexture(context.TEXTURE_2D, null);
  152. // Using 2d Canvas
  153. } else {
  154. if (slice.angle || slice.alpha !== 1 || slice.colorize || slice.mirrorX || slice.mirrorY) {
  155. context.save();
  156. restoreNeeded = true;
  157. }
  158. context.globalAlpha = slice.alpha;
  159. if (slice.angle) {
  160. context.translate(x, y);
  161. context.rotate(slice.angle * Math.PI / 180);
  162. context.translate(-x, -y);
  163. }
  164. // Careful about performance when using mirroring
  165. if (slice.mirrorX || slice.mirrorY) {
  166. context.translate(x, y);
  167. if (slice.mirrorX && slice.mirrorY) {
  168. context.scale(-1, -1);
  169. } else if (slice.mirrorX) {
  170. context.scale(-1, 1);
  171. } else {
  172. context.scale(1, -1);
  173. }
  174. context.translate(-x, -y);
  175. }
  176. if (slice.colorize) {
  177. if (!tmpCtx) {
  178. tmpCtx = document.createElement("canvas").getContext("2d");
  179. tmpCtx.canvas.width = context.canvas.width;
  180. tmpCtx.canvas.height = context.canvas.height;
  181. }
  182. context = tmpCtx;
  183. context.globalCompositeOperation = "copy";
  184. }
  185. // If the sprite is an animation
  186. if (slice.numberOfFrames > 1) {
  187. context.drawImage(
  188. imageResource.image,
  189. curFrameIndex * slice.width / slice.scale + slice.frameOffsetX,
  190. slice.frameOffsetY,
  191. slice.cropWidth || frameWidth,
  192. slice.cropHeight || frameHeight,
  193. x + slice.offsetX - cameraOffset.x,
  194. y + slice.offsetY - cameraOffset.y,
  195. slice.cropWidth || slice.width,
  196. slice.cropHeight || slice.height
  197. );
  198. // If the sprite is not an animation
  199. } else {
  200. context.drawImage(imageResource.image,
  201. slice.frameOffsetX,
  202. slice.frameOffsetY,
  203. slice.cropWidth || frameWidth,
  204. slice.cropHeight || frameHeight,
  205. x + slice.offsetX - cameraOffset.x,
  206. y + slice.offsetY - cameraOffset.y,
  207. slice.cropWidth || slice.width,
  208. slice.cropHeight || slice.height);
  209. }
  210. if (slice.colorize) {
  211. // Draw the color that should be overlayed over the image
  212. context.fillStyle = slice.colorize;
  213. context.globalCompositeOperation = "source-in";
  214. context.fillRect(x, y, slice.width, slice.height);
  215. context = slice.layer.ctx; // Change back from the temp context
  216. context.drawImage(tmpCtx.canvas, 0, 0);
  217. }
  218. if (restoreNeeded) {
  219. context.restore();
  220. }
  221. }
  222. if (drawBounds && context) {
  223. bounds = slice.getBounds();
  224. if (!bounds.length) {
  225. bounds = [bounds];
  226. }
  227. context.lineWidth = 4;
  228. for (i = 0; i < bounds.length; i += 1) {
  229. if (slice.dragging) {
  230. context.beginPath();
  231. context.fillStyle = "rgba(10, 255, 50, 0.4)";
  232. context.fillRect(bounds[i].x - cameraOffset.x, bounds[i].y - cameraOffset.y, bounds[i].width, bounds[i].height);
  233. context.closePath();
  234. } else if (slice.justTapped) {
  235. context.beginPath();
  236. context.fillStyle = "rgba(255, 10, 50, 0.4)";
  237. context.fillRect(bounds[i].x - cameraOffset.x, bounds[i].y - cameraOffset.y, bounds[i].width, bounds[i].height);
  238. context.closePath();
  239. } else if (slice.justNotTapped) {
  240. context.beginPath();
  241. context.fillStyle = "rgba(255, 10, 255, 0.4)";
  242. context.fillRect(bounds[i].x - cameraOffset.x, bounds[i].y - cameraOffset.y, bounds[i].width, bounds[i].height);
  243. context.closePath();
  244. } else if (slice.justReleased) {
  245. context.beginPath();
  246. context.fillStyle = "rgba(125, 10, 255, 0.4)";
  247. context.fillRect(bounds[i].x - cameraOffset.x, bounds[i].y - cameraOffset.y, bounds[i].width, bounds[i].height);
  248. context.closePath();
  249. slice.justReleased = false;
  250. }
  251. context.beginPath();
  252. context.strokeStyle = "rgba(96, 255, 0, 0.5)";
  253. context.strokeRect(bounds[i].x - cameraOffset.x, bounds[i].y - cameraOffset.y, bounds[i].width, bounds[i].height);
  254. context.closePath();
  255. }
  256. context.beginPath();
  257. context.arc(x - cameraOffset.x, y - cameraOffset.y, 7, 0, 2 * Math.PI, false);
  258. context.fillStyle = "rgba(96, 255, 0, 0.5)";
  259. context.fill();
  260. }
  261. }
  262. }
  263. }
  264. slice.dirty = false;
  265. };
  266. slice.gotoLastFrame = function () {
  267. if (curFrameIndex !== slice.numberOfFrames - 1) {
  268. curFrameIndex = slice.numberOfFrames - 1;
  269. slice.dirty = true;
  270. }
  271. };
  272. slice.gotoFrame = function (frameIndex) {
  273. var newFrameCnt = Math.floor(slice.frameDelay * (frameIndex - Math.floor(frameIndex)) / 100);
  274. frameIndex = Math.floor(frameIndex);
  275. if (curFrameIndex !== frameIndex || frameCnt !== newFrameCnt) {
  276. curFrameIndex = frameIndex;
  277. frameCnt = newFrameCnt;
  278. slice.dirty = true;
  279. }
  280. };
  281. slice.destroy = function () {
  282. if (slice) {
  283. slice.dispatchEvent("destroyed", slice);
  284. }
  285. imageResource = null;
  286. options = null;
  287. slice = null;
  288. };
  289. options = options || {};
  290. cropWidth = options.cropWidth;
  291. Object.defineProperty(slice, "cropWidth", {
  292. get: function () {
  293. return cropWidth;
  294. },
  295. set: function (value) {
  296. if (cropWidth !== value) {
  297. slice.dirty = true;
  298. cropWidth = value;
  299. }
  300. }
  301. });
  302. cropHeight = options.cropHeight;
  303. Object.defineProperty(slice, "cropHeight", {
  304. get: function () {
  305. return cropHeight;
  306. },
  307. set: function (value) {
  308. if (cropHeight !== value) {
  309. slice.dirty = true;
  310. cropHeight = value;
  311. }
  312. }
  313. });
  314. frameOffsetX = options.frameOffsetX || 0;
  315. Object.defineProperty(slice, "frameOffsetX", {
  316. get: function () {
  317. return frameOffsetX;
  318. },
  319. set: function (value) {
  320. if (frameOffsetX !== value) {
  321. slice.dirty = true;
  322. frameOffsetX = value;
  323. }
  324. }
  325. });
  326. frameOffsetY = options.frameOffsetY || 0;
  327. Object.defineProperty(slice, "frameOffsetY", {
  328. get: function () {
  329. return frameOffsetY;
  330. },
  331. set: function (value) {
  332. if (frameOffsetY !== value) {
  333. slice.dirty = true;
  334. frameOffsetY = value;
  335. }
  336. }
  337. });
  338. mirrorX = options.mirrorX;
  339. Object.defineProperty(slice, "mirrorX", {
  340. get: function () {
  341. return mirrorX;
  342. },
  343. set: function (value) {
  344. if (mirrorX !== value) {
  345. slice.dirty = true;
  346. mirrorX = value;
  347. }
  348. }
  349. });
  350. mirrorY = options.mirrorY;
  351. Object.defineProperty(slice, "mirrorY", {
  352. get: function () {
  353. return mirrorY;
  354. },
  355. set: function (value) {
  356. if (mirrorY !== value) {
  357. slice.dirty = true;
  358. mirrorY = value;
  359. }
  360. }
  361. });
  362. (function () {
  363. var image = options.image,
  364. imageSrc = options.imageSrc || (options.image && options.src),
  365. imagePreloaded = image ? true : false;
  366. options = options || {};
  367. // Pause the slice if autoPlay property is set to false
  368. if (!slice.autoPlay) {
  369. paused = true;
  370. }
  371. if (image || imageSrc) {
  372. imageResource = {
  373. image: image,
  374. imageSrc: imageSrc,
  375. loaded: imagePreloaded
  376. };
  377. // If the image is already loaded
  378. if (imageResource.loaded) {
  379. onResourceLoaded();
  380. } else {
  381. // If there is no image object
  382. if (!imageResource.image) {
  383. // Instantiate a new image
  384. imageResource.image = new Image();
  385. }
  386. imageResource.image.addEventListener("load", onResourceLoaded);
  387. imageResource.image.src = imageResource.imageSrc;
  388. }
  389. } else {
  390. onResourceLoaded();
  391. }
  392. }());
  393. return slice;
  394. };