1 | "use strict";
|
2 |
|
3 |
|
4 | var platform = require("./platform");
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function makeCanvas(width, height) {
|
14 | var c = document.createElement("canvas");
|
15 | c.width = width;
|
16 | c.height = height;
|
17 |
|
18 | if (platform.isEjecta()) {
|
19 | c.retinaResolutionEnabled = false;
|
20 | }
|
21 | return c;
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function makeBuffer(width, height, drawFun) {
|
32 | var canvas = makeCanvas(width, height);
|
33 | var ctx = canvas.getContext("2d");
|
34 |
|
35 | if (platform.isEjecta()) {
|
36 | ctx.imageSmoothingEnabled = false;
|
37 | }
|
38 | drawFun(ctx);
|
39 | return canvas;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function 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 |
|
56 |
|
57 |
|
58 |
|
59 | function 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 |
|
67 |
|
68 |
|
69 |
|
70 | function 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 |
|
83 |
|
84 |
|
85 |
|
86 | function 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 |
|
98 | module.exports = {
|
99 | makeBuffer: makeBuffer,
|
100 | flipBufferHorizontally: flipBufferHorizontally,
|
101 | flipBufferVertically: flipBufferVertically,
|
102 | rotateClockwise: rotateClockwise,
|
103 | rotateCounterclockwise: rotateCounterclockwise
|
104 | };
|