UNPKG

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