Mask image in html canvas

user3122094

Im trying to change a mask to an image in a html5 canvas when the mouse enters the canvas. When the mouse enters the canvas i draw a shape thats gonna act as a mask. It draws as i want but it wont mask the image.

The function that draws the mask is drawLine(); What is wrong here?

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var img = document.createElement('IMG');
var mY;
var mX;

img.onload = function () {
    context.save();
    context.beginPath();
    context.moveTo(0, 0);
    context.lineTo(160, 600);
    context.rect(0, 0, 160, 600);
    context.closePath();
    context.clip();
    context.drawImage(img, 0, 0);
    context.restore();
};

img.src = "http://placekitten.com/160/600";

canvas.addEventListener('mouseenter', function(evt) {
    var mousePos = getMousePos(canvas, evt);
    var message = mousePos.y + ', ' + mousePos.x; 
    mY = mousePos.y;
    mX = mousePos.x;
   drawLine();
}, false);

function getMousePos(canvas, evt) {
    var rect = canvas.getBoundingClientRect();
    return {
        x: evt.clientX - rect.left,
        y: evt.clientY - rect.top
    };
}

function drawLine() {

    if(mX > 80) {
        context.restore();
        context.fillStyle = "rgb(000,000,000)";
        context.beginPath();
        context.moveTo(160, mY);
        context.lineTo(0,mY+setCutAngle());
        context.lineTo(0,600);
        context.lineTo(160,600);
        context.fill();
        context.closePath();
    } else {

    }
    context.stroke();
}

function setCutAngle() {
    return Math.floor(Math.random() * 45) + 10;
}
PaulBinder

Is this what you are looking for? JSFIDDLE Updated so image loads at the start. I believe your main problem was caused by not clearing the canvas. Thus your clip would not show

Updated JSFIDDLE to match your code. Remove that logic from your load event. It should only happen on your drawevent. You are also not clearling or clipping your image in the draw event.

    var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var img = document.createElement('IMG');
var mY;
var mX;

img.onload = function () {
    context.drawImage(img, 0, 0);
};

img.src = "http://placekitten.com/160/600";

canvas.addEventListener('mouseenter', function(evt) {
    var mousePos = getMousePos(canvas, evt);
    var message = mousePos.y + ', ' + mousePos.x; 
    mY = mousePos.y;
    mX = mousePos.x;
    drawLine();
}, false);

function getMousePos(canvas, evt) {
    var rect = canvas.getBoundingClientRect();
    return {
        x: evt.clientX - rect.left,
        y: evt.clientY - rect.top
    };
}

function drawLine() {
     context.save();
    context.clearRect(0, 0, canvas.width, canvas.height);
    if(mX > 80) {
        context.restore();
        context.fillStyle = "rgb(000,000,000)";
        context.beginPath();
        context.moveTo(160, mY);
        context.lineTo(0,mY+setCutAngle());
        context.lineTo(0,600);
        context.lineTo(160,600);
        context.clip();
        context.drawImage(img, 0, 0);
        context.closePath();
    } else {

    }
    context.stroke()
}

function setCutAngle() {
    return Math.floor(Math.random() * 45) + 10;
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Adding text after image composite, html5 canvas

분류에서Dev

Animating SVG image mask on hover

분류에서Dev

Html2Canvas 다음 Canvas2Image가 생성 된 이미지 자르기

분류에서Dev

Gradient in HTML Canvas

분류에서Dev

Python - Image not being drawn on canvas

분류에서Dev

Rotate Rectangular Canvas Image to Fit

분류에서Dev

image resize and then rotate in canvas - javascript

분류에서Dev

HTML Canvas 총 기능

분류에서Dev

html5 canvas in durandal

분류에서Dev

HTML5 Canvas Pulsate

분류에서Dev

drawing polygon in canvas jquery html

분류에서Dev

Matterport Mask RCNN TRAIN_ROI_PER_IMAGE 설명?

분류에서Dev

saving canvas in php but no data in image file

분류에서Dev

Image width in canvas isnt being displayed correctly

분류에서Dev

Javascript Image Canvas return Data and Output

분류에서Dev

How to replace image in canvas using kinetic js

분류에서Dev

php와 html의 rewrite / mask URL 주소

분류에서Dev

html canvas - drawing circle with animation & number

분류에서Dev

Canvas HTML 5의 지우개

분류에서Dev

HTML 5 Canvas 숨기기?

분류에서Dev

HTML Canvas: Coloring Black and White Images Smoothly

분류에서Dev

HTML5 canvas globalAlpha not working

분류에서Dev

HTML5 Canvas Scale 위치

분류에서Dev

HTML Canvas Sniff Webgl 데이터

분류에서Dev

HTML 5 Canvas 및 QR 코드

분류에서Dev

Insert canvas context to html string in new dynamically created window html

분류에서Dev

With SurfaceView and 4 Canvas Images How to find which image was touched ?

분류에서Dev

HTML/CSS - image over image with parallax effect

분류에서Dev

HTML CSS Hover Image Link with One Image