Search code examples
javascripthtml5-canvaselectrongetusermedia

how to edit snapshot from a video?


I'm loading video from my computer through getUserMedia, I take a snapshot with canvas. But when i try to edit through of code below I can't.

var canvas = document.querySelector("canvas")
var ctx = canvas.getContext('2d');
var sketch = document.getElementById("sketch")
var sketch_style = getComputedStyle(sketch)
canvas.width = parseInt(sketch_style.getPropertyValue('width'))
canvas.height = parseInt(sketch_style.getPropertyValue('height'))
var mouse = {x: 0, y: 0}
var last_mouse = {x: 0, y: 0}

/* Mouse Capturing Work */
canvas.addEventListener('mousemove', function(e) {
    last_mouse.x = mouse.x;
    last_mouse.y = mouse.y;

    mouse.x = e.pageX - this.offsetLeft;
    mouse.y = e.pageY - this.offsetTop;
}, false);

/* Drawing on Paint App */
ctx.lineWidth = 5;
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
/* Posso fazer um switch case aqui pra escolher uma cor */
ctx.strokeStyle = 'blue';

canvas.addEventListener('mousedown', function(e) {
      canvas.addEventListener('mousemove', onPaint, false);
}, false);

canvas.addEventListener('mouseup', function() {
      canvas.removeEventListener('mousemove', onPaint, false);
}, false);

var onPaint = function() {
    ctx.beginPath();
    ctx.moveTo(last_mouse.x, last_mouse.y);
    ctx.lineTo(mouse.x, mouse.y);
    ctx.closePath();
    ctx.stroke();
};
function snapshot() {
    canvas.width = 500;
    canvas.height = 300;
    ctx.drawImage(videoNode, 0, 0, canvas.width, canvas.height);

}
foto.addEventListener('click', snapshot, false);

I can put lines, but they do not change the color, thickness, etc ... I am using the Electron to perform a desktop web application. Can someone help me?? :D

Snapshot on Electron


Solution

  • It's because when you set new size -even if it is the same size, the canvas will reset the state. can be fixed by setting line stuff after the new size is set

    function snapshot() {
        canvas.width = 500;
        canvas.height = 300;
        ctx.drawImage(videoNode, 0, 0, canvas.width, canvas.height);
    
        // set line stuff here <=====================
        ctx.lineWidth = 5;
        ctx.lineJoin = 'round';
        ctx.lineCap = 'round';
        /* Posso fazer um switch case aqui pra escolher uma cor */
        ctx.strokeStyle = 'blue';
    }