Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save escaroda/d3362b27709c24f15178b3ea90382788 to your computer and use it in GitHub Desktop.
Save escaroda/d3362b27709c24f15178b3ea90382788 to your computer and use it in GitHub Desktop.

Revisions

  1. @anupkrbid anupkrbid renamed this gist Jul 24, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @anupkrbid anupkrbid revised this gist Jul 1, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion drawing-board.componet.ts
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ import {
    ViewChild
    } from '@angular/core';
    import { fromEvent } from 'rxjs';
    import { switchMap, takeUntil, pairwise } from 'rxjs/operators';
    import { pairwise, switchMap, takeUntil } from 'rxjs/operators';

    @Component({
    selector: 'app-drawing-board',
  3. @anupkrbid anupkrbid revised this gist Jul 1, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions drawing-board.componet.ts
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,8 @@ import {
    Component,
    ElementRef,
    Input,
    ViewChild,
    OnDestroy
    OnDestroy,
    ViewChild
    } from '@angular/core';
    import { fromEvent } from 'rxjs';
    import { switchMap, takeUntil, pairwise } from 'rxjs/operators';
  4. @anupkrbid anupkrbid renamed this gist Jul 1, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @anupkrbid anupkrbid revised this gist Jul 1, 2018. 1 changed file with 11 additions and 4 deletions.
    15 changes: 11 additions & 4 deletions drawing.componet.ts
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,14 @@ import {
    Component,
    ElementRef,
    Input,
    ViewChild
    ViewChild,
    OnDestroy
    } from '@angular/core';
    import { fromEvent } from 'rxjs';
    import { switchMap, takeUntil, pairwise } from 'rxjs/operators';

    @Component({
    selector: 'app-canvas',
    selector: 'app-drawing-board',
    template: `<canvas #canvas></canvas>`,
    styles: [
    `
    @@ -19,11 +20,12 @@ import { switchMap, takeUntil, pairwise } from 'rxjs/operators';
    `
    ]
    })
    export class DrawingComponent implements AfterViewInit {
    export class DrawingBoardComponent implements AfterViewInit, OnDestroy {
    @Input() width = 512;
    @Input() height = 418;
    @ViewChild('canvas') canvas: ElementRef;
    cx: CanvasRenderingContext2D;
    drawingSubscription: Subscription;
    constructor() {}

    ngAfterViewInit() {
    @@ -46,7 +48,7 @@ export class DrawingComponent implements AfterViewInit {

    captureEvents(canvasEl: HTMLCanvasElement) {
    // this will capture all mousedown events from teh canvas element
    fromEvent(canvasEl, 'mousedown')
    this.drawingSubscription = fromEvent(canvasEl, 'mousedown')
    .pipe(
    switchMap(e => {
    // after a mouse down, we'll record all mouse moves
    @@ -104,4 +106,9 @@ export class DrawingComponent implements AfterViewInit {
    this.cx.stroke();
    }
    }

    ngOnDestroy() {
    // this will remove event lister when this component is destroyed
    this.drawingSubscription.unsubscribe();
    }
    }
  6. @anupkrbid anupkrbid renamed this gist Jul 1, 2018. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion ng-drawing.componet.ts → drawing.componet.ts
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ import { switchMap, takeUntil, pairwise } from 'rxjs/operators';
    `
    ]
    })
    export class VaultComponent implements AfterViewInit {
    export class DrawingComponent implements AfterViewInit {
    @Input() width = 512;
    @Input() height = 418;
    @ViewChild('canvas') canvas: ElementRef;
    @@ -45,12 +45,19 @@ export class VaultComponent implements AfterViewInit {
    }

    captureEvents(canvasEl: HTMLCanvasElement) {
    // this will capture all mousedown events from teh canvas element
    fromEvent(canvasEl, 'mousedown')
    .pipe(
    switchMap(e => {
    // after a mouse down, we'll record all mouse moves
    return fromEvent(canvasEl, 'mousemove').pipe(
    // we'll stop (and unsubscribe) once the user releases the mouse
    // this will trigger a 'mouseup' event
    takeUntil(fromEvent(canvasEl, 'mouseup')),
    // we'll also stop (and unsubscribe) once the mouse leaves the canvas (mouseleave event)
    takeUntil(fromEvent(canvasEl, 'mouseleave')),
    // pairwise lets us get the previous value to draw a line from
    // the previous point to the current point
    pairwise()
    );
    })
  7. @anupkrbid anupkrbid created this gist Jul 1, 2018.
    100 changes: 100 additions & 0 deletions ng-drawing.componet.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    import {
    AfterViewInit,
    Component,
    ElementRef,
    Input,
    ViewChild
    } from '@angular/core';
    import { fromEvent } from 'rxjs';
    import { switchMap, takeUntil, pairwise } from 'rxjs/operators';

    @Component({
    selector: 'app-canvas',
    template: `<canvas #canvas></canvas>`,
    styles: [
    `
    canvas {
    border: 1px solid #000;
    }
    `
    ]
    })
    export class VaultComponent implements AfterViewInit {
    @Input() width = 512;
    @Input() height = 418;
    @ViewChild('canvas') canvas: ElementRef;
    cx: CanvasRenderingContext2D;
    constructor() {}

    ngAfterViewInit() {
    // get the context
    const canvasEl: HTMLCanvasElement = this.canvas.nativeElement;
    this.cx = canvasEl.getContext('2d');

    // set the width and height
    canvasEl.width = this.width;
    canvasEl.height = this.height;

    // set some default properties about the line
    this.cx.lineWidth = 3;
    this.cx.lineCap = 'round';
    this.cx.strokeStyle = '#000';

    // we'll implement this method to start capturing mouse events
    this.captureEvents(canvasEl);
    }

    captureEvents(canvasEl: HTMLCanvasElement) {
    fromEvent(canvasEl, 'mousedown')
    .pipe(
    switchMap(e => {
    return fromEvent(canvasEl, 'mousemove').pipe(
    takeUntil(fromEvent(canvasEl, 'mouseup')),
    takeUntil(fromEvent(canvasEl, 'mouseleave')),
    pairwise()
    );
    })
    )
    .subscribe((res: [MouseEvent, MouseEvent]) => {
    const rect = canvasEl.getBoundingClientRect();

    // previous and current position with the offset
    const prevPos = {
    x: res[0].clientX - rect.left,
    y: res[0].clientY - rect.top
    };

    const currentPos = {
    x: res[1].clientX - rect.left,
    y: res[1].clientY - rect.top
    };

    // this method we'll implement soon to do the actual drawing
    this.drawOnCanvas(prevPos, currentPos);
    });
    }

    drawOnCanvas(
    prevPos: { x: number; y: number },
    currentPos: { x: number; y: number }
    ) {
    // incase the context is not set
    if (!this.cx) {
    return;
    }

    // start our drawing path
    this.cx.beginPath();

    // we're drawing lines so we need a previous position
    if (prevPos) {
    // sets the start point
    this.cx.moveTo(prevPos.x, prevPos.y); // from
    // draws a line from the start pos until the current position
    this.cx.lineTo(currentPos.x, currentPos.y);

    // strokes the current path with the styles we set earlier
    this.cx.stroke();
    }
    }
    }