UNPKG

1.69 kBJavaScriptView Raw
1"use strict";
2/** @module buffer */
3
4/**
5 * Make an invisible {@link canvas}.
6 * @param {number} width The width of the canvas
7 * @param {number} height The height of the canvas
8 * @returns {external:canvas} A canvas DOM element
9 * @private
10 */
11function makeCanvas(width, height) {
12 var c = document.createElement("canvas");
13 c.width = width;
14 c.height = height;
15 return c;
16}
17
18/**
19 * Make an invisible canvas buffer, and draw on it.
20 * @param {number} width The width of the buffer
21 * @param {number} height The height of the buffer
22 * @param {drawCallback} drawFun The callback that draws on the buffer
23 * @returns {external:canvas} The drawn buffer
24 */
25function makeBuffer(width, height, drawFun) {
26 var canvas = makeCanvas(width, height);
27 var ctx = canvas.getContext("2d");
28 drawFun(ctx);
29 return canvas;
30}
31
32/**
33 * Make a horizonally-flipped copy of a buffer or image.
34 * @param {external:canvas|external:image} buffer The original image
35 * @return {external:canvas} The flipped buffer
36 */
37function flipBufferHorizontally(buffer) {
38 return makeBuffer(buffer.width, buffer.height, function(context) {
39 context.scale(-1, 1);
40 context.drawImage(buffer, -buffer.width, 0);
41 });
42}
43
44/**
45 * Make a vertically-flipped copy of a buffer or image.
46 * @param {external:canvas|external:image} buffer The original image
47 * @return {external:canvas} The flipped buffer
48 */
49function flipBufferVertically(buffer) {
50 return makeBuffer(buffer.width, buffer.height, function(context) {
51 context.scale(1, -1);
52 context.drawImage(buffer, 0, -buffer.height);
53 });
54}
55
56module.exports = {
57 makeBuffer: makeBuffer,
58 flipBufferHorizontally: flipBufferHorizontally,
59 flipBufferVertically: flipBufferVertically
60};