/
BaseImage.ts
123 lines (112 loc) · 3.71 KB
/
BaseImage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright (C) 2016 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*/
import { createCanvas } from "../sys/canvas";
import { createImage } from "../sys/image";
/**
* Base class for all images.
*/
export abstract class BaseImage {
/** The image width in pixels. */
protected width: number;
/** The image height in pixels. */
protected height: number;
/**
* Creates a new image with the given size.
*
* @param width The image width in pixels.
* @param height The image height in pixels.
*/
protected constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
/**
* Returns the image width in pixels.
*
* @return The image width in pixels.
*/
public getWidth(): number {
return this.width;
}
/**
* Returns the image height in pixels.
*
* @return The image height in pixels.
*/
public getHeight(): number {
return this.height;
}
/**
* Returns the RGBA color at the specified position.
*
* @param x The horizontal pixel position.
* @param y The vertical pixel position.
* @return The RGBA color at the specified position.
*/
public abstract getColor(x: number, y: number): number;
/**
* Draws the image onto the given rendering context.
*
* @param ctx The rendering context to draw the image to.
* @param x Optional horizontal target position. Defaults to 0.
* @param y Optional vertical target position. Defaults to 0.
*/
public draw(ctx: CanvasRenderingContext2D, x: number = 0, y: number = 0): void {
const width = this.width;
const height = this.height;
const imageData = ctx.createImageData(width, height);
const pixels = imageData.data;
let rgbaIndex = 0;
for (let y = 0; y < height; ++y) {
for (let x = 0; x < width; ++x) {
const color = this.getColor(x, y);
pixels[rgbaIndex++] = (color >> 24) & 0xff;
pixels[rgbaIndex++] = (color >> 16) & 0xff;
pixels[rgbaIndex++] = (color >> 8) & 0xff;
pixels[rgbaIndex++] = color & 0xff;
}
}
ctx.putImageData(imageData, x, y);
}
/**
* Creates and returns a new canvas containing the image.
*
* @return The created canvas.
*/
public toCanvas(): HTMLCanvasElement {
const canvas = createCanvas(this.width, this.height);
const ctx = canvas.getContext("2d");
if (!ctx) {
throw new Error("Unable to create 2D rendering context");
}
this.draw(ctx);
return canvas;
}
/**
* Creates and returns an image data URL.
*
* @param type Optional image mime type. Defaults to image/png.
* @param args Optional additional encoder parameters. For image/jpeg this is the image quality between 0 and 1
* with a default value of 0.92.
* @return The created data URL.
*/
public toDataUrl(type?: string, ...args: any[]): string {
const canvas = this.toCanvas();
return canvas.toDataURL(type, ...args);
}
/**
* Creates and returns a HTML image.
*
* @param type Optional image mime type. Defaults to image/png.
* @param args Optional additional encoder parameters. For image/jpeg this is the image quality between 0 and 1
* with a default value of 0.92.
* @return The created HTML image.
*/
public toImage(type?: string, ...args: any[]): HTMLImageElement {
const image = createImage();
image.src = this.toDataUrl(type, ...args);
return image;
}
}