This repository has been archived by the owner on May 29, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
551 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {Directive, Inject, Input} from '@angular/core'; | ||
import {DrawService} from '../services/draw.service'; | ||
|
||
@Directive({ | ||
selector: '[waCanvasDrawImage]', | ||
providers: [DrawService], | ||
}) | ||
export class DrawImageDirective { | ||
@Input() | ||
waCanvasDrawImage?: CanvasImageSource; | ||
|
||
@Input() | ||
dX = 0; | ||
|
||
@Input() | ||
dY = 0; | ||
|
||
@Input() | ||
dWidth?: number; | ||
|
||
@Input() | ||
dHeight?: number; | ||
|
||
@Input() | ||
sX?: number; | ||
|
||
@Input() | ||
sY?: number; | ||
|
||
@Input() | ||
sWidth?: number; | ||
|
||
@Input() | ||
sHeight?: number; | ||
|
||
constructor(@Inject(DrawService) drawService: DrawService) { | ||
drawService.init(context => { | ||
if (!this.waCanvasDrawImage) { | ||
return; | ||
} | ||
|
||
if ( | ||
this.sWidth !== undefined && | ||
this.sHeight !== undefined && | ||
this.sX !== undefined && | ||
this.sY !== undefined && | ||
this.dWidth !== undefined && | ||
this.dHeight !== undefined | ||
) { | ||
context.drawImage( | ||
this.waCanvasDrawImage, | ||
this.sX, | ||
this.sY, | ||
this.sWidth, | ||
this.sHeight, | ||
this.dX, | ||
this.dY, | ||
this.dWidth, | ||
this.dHeight, | ||
); | ||
} else if (this.dWidth !== undefined && this.dHeight !== undefined) { | ||
context.drawImage( | ||
this.waCanvasDrawImage, | ||
this.dX, | ||
this.dY, | ||
this.dWidth, | ||
this.dHeight, | ||
); | ||
} else { | ||
context.drawImage(this.waCanvasDrawImage, this.dX, this.dY); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {Directive, Inject, Input} from '@angular/core'; | ||
import {DrawService} from '../services/draw.service'; | ||
|
||
@Directive({ | ||
selector: '[waCanvasFillRect]', | ||
providers: [DrawService], | ||
}) | ||
export class FillRectDirective { | ||
@Input() | ||
x = 0; | ||
|
||
@Input() | ||
y = 0; | ||
|
||
@Input() | ||
width = 0; | ||
|
||
@Input() | ||
height = 0; | ||
|
||
constructor(@Inject(DrawService) drawService: DrawService) { | ||
drawService.init(context => { | ||
context.fillRect(this.x, this.y, this.width, this.height); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import {Component, ViewChild} from '@angular/core'; | ||
import {ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {CanvasModule} from '../../module'; | ||
import {CANVAS_RENDERING_CONTEXT} from '../../tokens/canvas-rendering-context'; | ||
|
||
describe('Canvas2dDirective', () => { | ||
@Component({ | ||
template: ` | ||
<canvas #canvas waCanvas2d width="100" height="100"> | ||
<ng-container [waCanvasDrawImage]="image"></ng-container> | ||
<ng-container | ||
[waCanvasDrawImage]="image" | ||
[dX]="10" | ||
[dY]="10" | ||
[dWidth]="10" | ||
[dHeight]="10" | ||
></ng-container> | ||
</canvas> | ||
`, | ||
}) | ||
class TestComponent { | ||
@ViewChild('canvas', {read: CANVAS_RENDERING_CONTEXT}) | ||
readonly context!: CanvasRenderingContext2D; | ||
|
||
readonly image = new Image(); | ||
|
||
constructor() { | ||
this.image.src = | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg=='; | ||
} | ||
} | ||
|
||
let fixture: ComponentFixture<TestComponent>; | ||
let testComponent: TestComponent; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [CanvasModule], | ||
declarations: [TestComponent], | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(TestComponent); | ||
testComponent = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
afterAll(() => { | ||
fixture.destroy(); | ||
}); | ||
|
||
it('draws an image', done => { | ||
setTimeout(() => { | ||
expect([...testComponent.context.getImageData(1, 1, 1, 1).data]).toEqual([ | ||
0, | ||
0, | ||
0, | ||
0, | ||
]); | ||
expect([...testComponent.context.getImageData(0, 0, 1, 1).data]).toEqual([ | ||
255, | ||
0, | ||
0, | ||
255, | ||
]); | ||
done(); | ||
}, 50); | ||
}); | ||
|
||
it('draws an image with offset and scale', done => { | ||
setTimeout(() => { | ||
expect([...testComponent.context.getImageData(10, 10, 1, 1).data]).toEqual([ | ||
255, | ||
0, | ||
0, | ||
255, | ||
]); | ||
expect([...testComponent.context.getImageData(19, 19, 1, 1).data]).toEqual([ | ||
255, | ||
0, | ||
0, | ||
255, | ||
]); | ||
expect([...testComponent.context.getImageData(20, 20, 1, 1).data]).toEqual([ | ||
0, | ||
0, | ||
0, | ||
0, | ||
]); | ||
done(); | ||
}, 50); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {Context2dProcessor} from '../types/context-processor'; | ||
|
||
export interface CanvasDrawStep { | ||
readonly beforeHook: Context2dProcessor; | ||
readonly afterHook: Context2dProcessor; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
import {NgModule} from '@angular/core'; | ||
import {Canvas2dDirective} from './directives/canvas-2d'; | ||
import {DrawImageDirective} from './directives/draw-image'; | ||
import {FillRectDirective} from './directives/fill-rect'; | ||
import {FillStyleDirective} from './steps/fill-style'; | ||
import {FilterDirective} from './steps/filter'; | ||
|
||
@NgModule({ | ||
declarations: [Canvas2dDirective], | ||
exports: [Canvas2dDirective], | ||
declarations: [ | ||
Canvas2dDirective, | ||
DrawImageDirective, | ||
FillRectDirective, | ||
FillStyleDirective, | ||
FilterDirective, | ||
], | ||
exports: [ | ||
Canvas2dDirective, | ||
DrawImageDirective, | ||
FillRectDirective, | ||
FillStyleDirective, | ||
FilterDirective, | ||
], | ||
}) | ||
export class CanvasModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {Inject, Injectable, OnDestroy} from '@angular/core'; | ||
import {ANIMATION_FRAME} from '@ng-web-apis/common'; | ||
import {Observable, Subscription} from 'rxjs'; | ||
import {CanvasDrawStep} from '../interfaces/canvas-draw-step'; | ||
import {CANVAS_DRAW_STEPS} from '../tokens/canvas-draw-steps'; | ||
import {CANVAS_RENDERING_CONTEXT} from '../tokens/canvas-rendering-context'; | ||
import {Context2dProcessor} from '../types/context-processor'; | ||
|
||
// @dynamic | ||
@Injectable() | ||
export class DrawService implements OnDestroy { | ||
private draw: Context2dProcessor = () => {}; | ||
|
||
private readonly subscription: Subscription; | ||
|
||
constructor( | ||
@Inject(CANVAS_DRAW_STEPS) steps: CanvasDrawStep[], | ||
@Inject(CANVAS_RENDERING_CONTEXT) context: CanvasRenderingContext2D, | ||
@Inject(ANIMATION_FRAME) animationFrame$: Observable<number>, | ||
) { | ||
this.subscription = animationFrame$.subscribe(() => { | ||
steps.forEach(step => step.beforeHook(context)); | ||
this.draw(context); | ||
steps.forEach(step => step.afterHook(context)); | ||
}); | ||
} | ||
|
||
init(draw: Context2dProcessor) { | ||
this.draw = draw; | ||
} | ||
|
||
ngOnDestroy() { | ||
this.subscription.unsubscribe(); | ||
} | ||
} |
Oops, something went wrong.