Skip to content

Commit d4c6e78

Browse files
committed
adding gradient brush
1 parent d53e761 commit d4c6e78

File tree

5 files changed

+101
-3
lines changed

5 files changed

+101
-3
lines changed

example/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ <h3># Drawing Board</h3>
4848
<label for="draw-tool">Tool: </label>
4949
<select name="draw-tool" id="draw-tool" value="brush">
5050
<option value="brush">Brush</option>
51+
<option value="gradientBrush">Gradient Brush</option>
5152
<option value="eraser">Eraser</option>
5253
<option value="line">Line</option>
5354
</select>

src/renderers/RealDrawBoard/RealDrawBoard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class RealDrawBoard extends RealRenderer {
3939
eraserSize: number;
4040
tool: Tool = RealDrawBoardDefaults.tool;
4141
_isDrawing: boolean = false;
42-
_: boolean = false; // If a tool is drawing a strwoke
42+
_: boolean = false; // If a tool is drawing a stroke
4343
_snapshots: (number[][][])[] = []; // Undo snapshots
4444
_currentSnapshotIndex = 0; // Current snapshot
4545
_maxSnapshots: number;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { RealDrawBoard } from '../RealDrawBoard';
2+
import { Texture } from 'gpu.js';
3+
import { convertHSLToRGB } from '../../../util/convertForm';
4+
import { type } from 'os';
5+
6+
var hue: number = 0;
7+
var gradientColors: [number ,number ,number] = [ 1 ,1 ,1 ];
8+
9+
export const name = 'gradientBrush';
10+
11+
export function _startStroke(
12+
this: RealDrawBoard,
13+
coords: [number, number],
14+
identifier: string
15+
) {
16+
gradientColors = convertHSLToRGB(hue,90,40);
17+
this._doPreview = false;
18+
this._plot(coords[0], coords[1], this.brushSize, gradientColors);
19+
}
20+
21+
export function _endStroke(
22+
this: RealDrawBoard,
23+
endCoords: [number, number],
24+
identifier: string
25+
) {
26+
gradientColors = convertHSLToRGB(hue,90,40);
27+
this._plot(endCoords[0], endCoords[1], this.brushSize, gradientColors);
28+
this._doPreview = true;
29+
}
30+
31+
export function _doStroke(
32+
this: RealDrawBoard,
33+
coords: [number, number],
34+
identifier: string
35+
) {
36+
hue = (hue+1)%360;
37+
gradientColors = convertHSLToRGB(hue,90,40);
38+
// this.changeBrushColor(gradientColors);
39+
this._plot(coords[0], coords[1], this.brushSize, gradientColors);
40+
this._stroke(coords[0], coords[1], this.brushSize, gradientColors, identifier);
41+
}
42+
43+
export function _toolPreview(
44+
this: RealDrawBoard,
45+
coords: [number, number],
46+
identifier: string
47+
): Texture {
48+
return <Texture>this._previewPlot(
49+
this.graphPixels,
50+
coords[0],
51+
coords[1],
52+
this.brushSize,
53+
gradientColors
54+
)
55+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import * as brush from './brush';
22
import * as eraser from './eraser';
33
import * as line from './line';
4+
import * as gradientBrush from './gradientBrush';
45

56
export const tools = {
67
brush,
8+
gradientBrush,
79
eraser,
810
line
911
}
1012

11-
export type Tool = 'brush' | 'eraser' | 'line';
13+
export type Tool = 'brush' | 'gradientBrush' | 'eraser' | 'line';

src/util/convertForm.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,51 @@ export function convertPolarCartesian(r: number, theta: number) {
1313
/**
1414
* Convert Cartesian to polar form.
1515
* @param x Real Part
16-
* @param theta Complex Part
16+
* @param y Complex Part
1717
*/
1818
export function convertCartesianPolar(x: number, y: number) {
1919
return [
2020
Math.sqrt(x*x + y*y),
2121
Math.atan2(y, x)
2222
]
23+
}
24+
25+
/**
26+
* Convert hsl Color into RGB color.
27+
* @param h color value ranges from 0 to 360.
28+
* @param s saturation value.
29+
* @param l brightness level.
30+
* @returns array containing r g and b value
31+
*/
32+
export function convertHSLToRGB(h: number, s: number, l: number) :[ number ,number ,number ] {
33+
34+
s /= 100;
35+
l /= 100;
36+
37+
var c: number = (1 - Math.abs(2 * l - 1)) * s;
38+
var x: number = c * (1 - Math.abs((h / 60) % 2 - 1));
39+
var m: number = l - c/2;
40+
var r: number = 0;
41+
var g: number = 0;
42+
var b: number = 0;
43+
44+
if (0 <= h && h < 60) {
45+
r = c; g = x; b = 0;
46+
} else if (60 <= h && h < 120) {
47+
r = x; g = c; b = 0;
48+
} else if (120 <= h && h < 180) {
49+
r = 0; g = c; b = x;
50+
} else if (180 <= h && h < 240) {
51+
r = 0; g = x; b = c;
52+
} else if (240 <= h && h < 300) {
53+
r = x; g = 0; b = c;
54+
} else if (300 <= h && h < 360) {
55+
r = c; g = 0; b = x;
56+
}
57+
58+
r = Math.round((r + m) * 255);
59+
g = Math.round((g + m) * 255);
60+
b = Math.round((b + m) * 255);
61+
62+
return [ r ,g ,b ];
2363
}

0 commit comments

Comments
 (0)