How to perform undo task in canvas draw component (typescript)

am making one application in which task is drawing on image which is placed on canvas…drawing on image works well …but how can i performed undo and redo operation in canvas code or function please my ts file code

import { Component, ViewChild, Renderer } from ‘@angular/core’;
import { Platform } from ‘ionic-angular’;
import { Camera, CameraOptions } from ‘@ionic-native/camera’;
import { IonicPage, NavController, NavParams } from ‘ionic-angular’;
import { AlertController} from ‘ionic-angular’;

@Component({
selector: ‘canvas-draw’,
templateUrl: ‘canvas-draw.html’
})
export class CanvasDraw {

@ViewChild('myCanvas') canvas: any ;

CANVAS_width= 500;
CANVAS_height= 700;
canvasElement: any;
lastX: number;
lastY: number;
public photos : any;
base64Image:any;

currentColour: string = '#1abc9c';
availableColours: any;

brushSize: number = 10;

constructor(public platform: Platform, public renderer: Renderer,private camera : Camera,public navCtrl: NavController, public navParams: NavParams,private alertCtrl: AlertController) {
    console.log('Hello CanvasDraw Component');

    this.availableColours = [
        '#1abc9c',
        '#3498db',
        '#9b59b6',
        '#e67e22',
        '#e74c3c'
    ];

}


ngAfterViewInit(){

    this.canvasElement = this.canvas.nativeElement;

    this.renderer.setElementAttribute(this.canvasElement, 'width', this.platform.width() + '');
    this.renderer.setElementAttribute(this.canvasElement, 'height', this.platform.height() + '');

}
ngOnInit() {
    this.photos = [];
  }

changeColour(colour){
    this.currentColour = colour;
}

changeSize(size){
    this.brushSize = size;
}

handleStart(ev){

    this.lastX = ev.touches[0].pageX;
    this.lastY = ev.touches[0].pageY;
}

handleMove(ev){

    let ctx = this.canvasElement.getContext('2d');
    let currentX = ev.touches[0].pageX;
    let currentY = ev.touches[0].pageY;

    ctx.beginPath();
    ctx.lineJoin = "round";
    ctx.moveTo(this.lastX, this.lastY);
    ctx.lineTo(currentX, currentY);
    ctx.closePath();
    ctx.strokeStyle = this.currentColour;
    ctx.lineWidth = this.brushSize;
    ctx.stroke();      

    this.lastX = currentX;
    this.lastY = currentY;

}

clearCanvas(){
    let ctx = this.canvasElement.getContext('2d');
    ctx.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height); 
    this.canvasElement.addEventListener('touchstart', ev => this.handleStart(ev));
    this.canvasElement.addEventListener('touchmove', ev => this.handleMove(ev));  
}


takePhoto() 
{

  
  var image = new Image();

  let ctx = this.canvasElement.getContext('2d');
 
  const options : CameraOptions = 
  {
    quality: 50, // picture quality
    destinationType: this.camera.DestinationType.DATA_URL,
    encodingType: this.camera.EncodingType.JPEG,
    mediaType: this.camera.MediaType.PICTURE
  }
  this.camera.getPicture(options).then((imageData) => 
  {
        

      this.base64Image = "data:image/jpeg;base64," + imageData;
      //this.photos.push(this.base64Image);
      //this.photos.reverse();



      //canvas.push(this.base64Image);

      image.src=this.base64Image;
      

     // ctx.drawImage(image, 0, 0);

    //   ctx.drawImage(image, 0, 0, image.width,image.height,     // source rectangle
    //       0, 0, this.canvasElement.width, this.canvasElement.height);

          ctx.drawImage(image, 0, 0, image.width,image.height,     // source rectangle
            0, 0,  this.CANVAS_width, this.CANVAS_height);

          this.canvasElement.addEventListener('touchstart', ev => this.handleStart(ev));

this.canvasElement.addEventListener(‘touchmove’, ev => this.handleMove(ev));

    }, (err) => {
      console.log(err);
    });

}

}

no one have solution for this task ? :smile:

I haven’t really used canvas, but saving all the modifications would be the solution.
When the user wants to undo something, clear the canvas and execute every modification except the last one.

Thanks @Matte for solution can you please check my code
canvas-draw.ts file
import { Component, ViewChild, Renderer } from ‘@angular/core’;
import { Platform } from ‘ionic-angular’;
import { Camera, CameraOptions } from ‘@ionic-native/camera’;
import { IonicPage, NavController, NavParams } from ‘ionic-angular’;
import { AlertController} from ‘ionic-angular’;

@Component({
selector: ‘canvas-draw’,
templateUrl: ‘canvas-draw.html’
})
export class CanvasDraw {
// touchstartListener = (ev) => this.handleStart((ev));
// touchmoveListener = (ev) => this.handleMove((ev));

@ViewChild('myCanvas') canvas: any ;

CANVAS_width= 500;
CANVAS_height= 700;
canvasElement: any;
lastX: number;
lastY: number;
public photos : any;
base64Image:any;

currentColour: string = '#1abc9c';
availableColours: any;

brushSize: number = 10;

constructor(public platform: Platform, public renderer: Renderer,private camera : Camera,public navCtrl: NavController, public navParams: NavParams,private alertCtrl: AlertController) {
    console.log('Hello CanvasDraw Component');

    this.availableColours = [
        '#1abc9c',
        '#3498db',
        '#9b59b6',
        '#e67e22',
        '#e74c3c'
    ];

}


ngAfterViewInit(){

    this.canvasElement = this.canvas.nativeElement;

    this.renderer.setElementAttribute(this.canvasElement, 'width', this.platform.width() + '');
    this.renderer.setElementAttribute(this.canvasElement, 'height', this.platform.height() + '');

}
ngOnInit() {
    this.photos = [];
  }

changeColour(colour){
    this.currentColour = colour;
}

changeSize(size){
    this.brushSize = size;
}

handleStart(ev){

    this.lastX = ev.touches[0].pageX;
    this.lastY = ev.touches[0].pageY;
}

handleMove(ev){

    let ctx = this.canvasElement.getContext('2d');
    let currentX = ev.touches[0].pageX;
    let currentY = ev.touches[0].pageY;

    ctx.beginPath();
    ctx.lineJoin = "round";
    ctx.moveTo(this.lastX, this.lastY);
    ctx.lineTo(currentX, currentY);
    ctx.closePath();
    ctx.strokeStyle = this.currentColour;
    ctx.lineWidth = this.brushSize;
    ctx.stroke();      

    this.lastX = currentX;
    this.lastY = currentY;

}

clearCanvas(){
    let ctx = this.canvasElement.getContext('2d');
    ctx.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height); 
    this.canvasElement.addEventListener('touchstart', ev => this.handleStart(ev));
    this.canvasElement.addEventListener('touchmove', ev => this.handleMove(ev));  
}



takePhoto() 
{

  
  var image = new Image();

  let ctx = this.canvasElement.getContext('2d');
 
  const options : CameraOptions = 
  {
    quality: 50, // picture quality
    destinationType: this.camera.DestinationType.DATA_URL,
    encodingType: this.camera.EncodingType.JPEG,
    mediaType: this.camera.MediaType.PICTURE
  }
  this.camera.getPicture(options).then((imageData) => 
  {
        

      this.base64Image = "data:image/jpeg;base64," + imageData;
    


    

      image.src=this.base64Image;
      

    

    
          ctx.drawImage(image, 0, 0, image.width,image.height,     // source rectangle
            0, 0,  this.CANVAS_width, this.CANVAS_height);

          this.canvasElement.addEventListener('touchstart', ev => this.handleStart(ev));

this.canvasElement.addEventListener(‘touchmove’, ev => this.handleMove(ev));

    }, (err) => {
      console.log(err);
    });


}

}

i am drawing on image which is placed on canvas…drawing is working …but not able to use use undo operation…i use clear canvas method but that clear entire drawing on image so solution please

@MattE now am able to perform undo on the drawing on signature-pad …but when i display image on signature-pad and then draw on image and if i perform undo operation …that clear entire image …i want to undo only data on image and not image
my code is
perform()
{
var data = this.signaturePad.toData();
if (data) {

      data.pop(); // remove the last dot or line
     this.signaturePad.fromData(data);
  }
   }

Hi,

have you found a solution? I’m facing the same issue. can you help me ?

Check this links

https://youtu.be/P6SLf6jmXS0

https://youtu.be/VXcdI_grUAA

Hope you will get solution .

If not email me umeshshelke2014@gmail.com

thanks !! will give a try