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