UNPKG

3.26 kBJavaScriptView Raw
1/** @module splat-ecs/lib/buffer */
2
3var platform = require("./platform");
4
5/**
6 * Make an invisible {@link canvas}.
7 * @param {number} width The width of the canvas
8 * @param {number} height The height of the canvas
9 * @returns {external:canvas} A canvas DOM element
10 * @private
11 */
12function makeCanvas(width, height) {
13 var c = document.createElement("canvas");
14 c.width = width;
15 c.height = height;
16 // when retina support is enabled, context.getImageData() reads from the wrong pixel causing NinePatch to break
17 if (platform.isEjecta()) {
18 c.retinaResolutionEnabled = false;
19 }
20 return c;
21}
22
23/**
24 * Make an invisible canvas buffer, and draw on it.
25 * @param {number} width The width of the buffer
26 * @param {number} height The height of the buffer
27 * @param {drawCallback} drawFun The callback that draws on the buffer
28 * @returns {external:canvas} The drawn buffer
29 */
30function makeBuffer(width, height, drawFun) {
31 var canvas = makeCanvas(width, height);
32 var ctx = canvas.getContext("2d");
33 // when image smoothing is enabled, the image gets blurred and the pixel data isn't correct even when the image shouldn't be scaled which breaks NinePatch
34 if (platform.isEjecta()) {
35 ctx.imageSmoothingEnabled = false;
36 }
37 drawFun(ctx);
38 return canvas;
39}
40
41/**
42 * Make a horizonally-flipped copy of a buffer or image.
43 * @param {external:canvas|external:image} buffer The original image
44 * @return {external:canvas} The flipped buffer
45 */
46function flipBufferHorizontally(buffer) {
47 return makeBuffer(buffer.width, buffer.height, function(context) {
48 context.scale(-1, 1);
49 context.drawImage(buffer, -buffer.width, 0);
50 });
51}
52
53/**
54 * Make a vertically-flipped copy of a buffer or image.
55 * @param {external:canvas|external:image} buffer The original image
56 * @return {external:canvas} The flipped buffer
57 */
58function flipBufferVertically(buffer) {
59 return makeBuffer(buffer.width, buffer.height, function(context) {
60 context.scale(1, -1);
61 context.drawImage(buffer, 0, -buffer.height);
62 });
63}
64/**
65 * Make a copy of a buffer that is rotated 90 degrees clockwise.
66 * @param {external:canvas|external:image} buffer The original image
67 * @return {external:canvas} The rotated buffer
68 */
69function rotateClockwise(buffer) {
70 var w = buffer.height;
71 var h = buffer.width;
72 var w2 = Math.floor(w / 2);
73 var h2 = Math.floor(h / 2);
74 return makeBuffer(w, h, function(context) {
75 context.translate(w2, h2);
76 context.rotate(Math.PI / 2);
77 context.drawImage(buffer, -h2, -w2);
78 });
79}
80/**
81 * Make a copy of a buffer that is rotated 90 degrees counterclockwise.
82 * @param {external:canvas|external:image} buffer The original image
83 * @return {external:canvas} The rotated buffer
84 */
85function rotateCounterclockwise(buffer) {
86 var w = buffer.height;
87 var h = buffer.width;
88 var w2 = Math.floor(w / 2);
89 var h2 = Math.floor(h / 2);
90 return makeBuffer(w, h, function(context) {
91 context.translate(w2, h2);
92 context.rotate(-Math.PI / 2);
93 context.drawImage(buffer, -h2, -w2);
94 });
95}
96
97module.exports = {
98 makeBuffer: makeBuffer,
99 flipBufferHorizontally: flipBufferHorizontally,
100 flipBufferVertically: flipBufferVertically,
101 rotateClockwise: rotateClockwise,
102 rotateCounterclockwise: rotateCounterclockwise
103};