Skip to content

Commit

Permalink
feat: dom shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Feb 1, 2024
1 parent f5823e2 commit 89c9299
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/canvas/Dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './Paint';
export * from './Fill';
export * from './Text';
export * from './Shadow';
export * from './shaders';
3 changes: 3 additions & 0 deletions packages/canvas/Dom/shaders/Gradients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Paint } from '../Paint';

export class Gradients extends Paint {}
45 changes: 45 additions & 0 deletions packages/canvas/Dom/shaders/LinearGradient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Property } from '@nativescript/core';
import { Gradients } from './Gradients';

const startProperty = new Property<LinearGradient, { x: number; y: number }>({
name: 'start',
});

const endProperty = new Property<LinearGradient, { x: number; y: number }>({
name: 'end',
});

const colorsProperty = new Property<LinearGradient, string[]>({
name: 'colors',
});

export class LinearGradient extends Gradients {
start: { x: number; y: number };
end: { x: number; y: number };
colors: string[];

_getColor() {
const ctx = this._canvas.getContext('2d');
const gradient = ctx.createLinearGradient(this.start.x, this.start.y, this.end.x, this.end.y);

for (let i = 0; i < this.colors.length; i++) {
gradient.addColorStop(i, this.colors[i]);
}

return gradient;
}

draw(): void {
const ctx = this._canvas.getContext('2d');
const gradient = this._getColor();
if (this._getPaintStyle() === 'stroke') {
ctx.strokeStyle = gradient;
} else {
ctx.fillStyle = gradient;
}
}
}

startProperty.register(LinearGradient);
endProperty.register(LinearGradient);
colorsProperty.register(LinearGradient);
59 changes: 59 additions & 0 deletions packages/canvas/Dom/shaders/TwoPointConicalGradient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Property } from '@nativescript/core';
import { Gradients } from './Gradients';

const startProperty = new Property<TwoPointConicalGradient, { x: number; y: number }>({
name: 'start',
});

const startRProperty = new Property<TwoPointConicalGradient, number>({
name: 'startR',
valueConverter: parseFloat
});

const endProperty = new Property<TwoPointConicalGradient, { x: number; y: number }>({
name: 'end',
});

const endRProperty = new Property<TwoPointConicalGradient, number>({
name: 'endR',
valueConverter: parseFloat
});

const colorsProperty = new Property<TwoPointConicalGradient, string[]>({
name: 'colors',
});

export class TwoPointConicalGradient extends Gradients {
start: { x: number; y: number };
end: { x: number; y: number };
colors: string[];
startR: number;
endR: number;

_getColor() {
const ctx = this._canvas.getContext('2d');
const gradient = ctx.createRadialGradient(this.start.x, this.start.y, this.startR, this.end.x, this.end.y, this.endR);

for (let i = 0; i < this.colors.length; i++) {
gradient.addColorStop(i, this.colors[i]);
}

return gradient;
}

draw(): void {
const ctx = this._canvas.getContext('2d');
const gradient = this._getColor();
if (this._getPaintStyle() === 'stroke') {
ctx.strokeStyle = gradient;
} else {
ctx.fillStyle = gradient;
}
}
}

startProperty.register(TwoPointConicalGradient);
endProperty.register(TwoPointConicalGradient);
colorsProperty.register(TwoPointConicalGradient);
startRProperty.register(TwoPointConicalGradient);
endRProperty.register(TwoPointConicalGradient);
2 changes: 2 additions & 0 deletions packages/canvas/Dom/shaders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './LinearGradient';
export * from './TwoPointConicalGradient';
1 change: 1 addition & 0 deletions packages/canvas/Dom/shapes/Circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class Circle extends Paint {
this.nativeView.addView(this._canvas.nativeView);
return true;
} else if (view instanceof Paint) {
view._canvas = this._canvas;
this._children.push(view);
}
return false;
Expand Down
8 changes: 5 additions & 3 deletions packages/canvas/Dom/shapes/Line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ export class Line extends Paint {
const context = this._canvas.getContext('2d') as any as CanvasRenderingContext2D;
const line = new Path2D();
line.lineTo(this.p1, this.p2);
const style = this.paintStyle;
const color = this._getColor();
const style = this._getPaintStyle();

if (style === 'fill') {
context.fillStyle = this.color.hex;
context.fillStyle = color;
context.fill(line);
} else if (style === 'stroke') {
context.strokeStyle = this.color.hex;
context.strokeStyle = color;
context.stroke(line);
}
}
Expand Down
16 changes: 11 additions & 5 deletions packages/canvas/Dom/shapes/Rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ export class Rect extends Paint {
context.rect(this.x, this.y, this.width, this.height);
if (this._children.length > 0) {
for (const child of this._children) {
switch (child.paintStyle) {
const color = child._getColor();
const style = child._getPaintStyle();
switch (style) {
case 'fill':
context.fillStyle = child.color.hex;
context.fillStyle = color;
context.fill();
break;
case 'stroke':
context.strokeStyle = child.color.hex;
context.lineWidth = child.strokeWidth;
context.strokeStyle = color;
context.lineWidth = child._getStrokeWidth();
context.stroke();
break;
}
Expand All @@ -61,7 +63,11 @@ export class Rect extends Paint {
}

_addViewToNativeVisualTree(view: ViewBase, atIndex?: number): boolean {
if (view instanceof Paint) {
if (view === this._canvas) {
this.nativeView.addView(this._canvas.nativeView);
return true;
} else if (view instanceof Paint) {
view._canvas = this._canvas;
this._children.push(view);
}
return false;
Expand Down

0 comments on commit 89c9299

Please sign in to comment.