sprite.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. function sprite(options) {
  2. let that = {},
  3. frameIndex = 0,
  4. tickCount = 0,
  5. ticksPerFrame = options.ticksPerFrame || 0,
  6. numberOfFrames = options.numberOfFrames || 1,
  7. isLastFrame = false;
  8. that.videofenbianlv = options.videofenbianlv || 1080/1080;
  9. that.context = options.context;
  10. that.width = options.width;
  11. that.height = options.height;
  12. that.drawWidth = options.drawWidth;
  13. that.drawHeight = options.drawHeight;
  14. that.x = 0;
  15. that.y = 0;
  16. that.image = options.image;
  17. that.workVideo = options.workVideo && options.workVideo[0];
  18. that.scaleRatio = options.scale;
  19. that.setup = function () {
  20. isLastFrame = false;
  21. frameIndex = 0;
  22. if (that.workVideo) {
  23. that.workVideo.addEventListener("ended", function () {
  24. isLastFrame = true;
  25. that.workVideo.src = '';
  26. });
  27. }
  28. };
  29. that.update = function () {
  30. // tickCount += 1;
  31. // if (tickCount > ticksPerFrame) {
  32. // tickCount = 0;
  33. // if (frameIndex < numberOfFrames - 1) {
  34. // frameIndex += 1;
  35. // isLastFrame = false;
  36. // } else {
  37. // //frameIndex = 0;
  38. // isLastFrame = true;
  39. // }
  40. // }
  41. };
  42. that.render = function () {
  43. // that.context.drawImage(
  44. // that.image,
  45. // frameIndex * that.width / numberOfFrames,
  46. // 0,
  47. // that.width / numberOfFrames,
  48. // that.height,
  49. // that.x,
  50. // that.y,
  51. // that.drawWidth,
  52. // that.drawHeight);
  53. // console.log(that.workVideo);
  54. if (that.workVideo) {
  55. if (that.workVideo.paused && that.workVideo.src) {
  56. that.workVideo.play();
  57. }
  58. that.context.drawImage(that.workVideo, 0, 0, that.drawHeight * that.videofenbianlv, that.drawHeight);
  59. }
  60. };
  61. that.isAnimStop = function () {
  62. return isLastFrame;
  63. };
  64. that.setPosition = function (x, y) {
  65. that.x = x;
  66. that.y = y;
  67. };
  68. that.getPosition = function () {
  69. return { x: that.x, y: that.y };
  70. };
  71. that.getSize = function () {
  72. return { width: that.width, height: that.height };
  73. };
  74. return that;
  75. }