如何使用HTML 5画布仅绘制图像的圆形部分?

Red*_*gon 3 javascript canvas html5-canvas

如何使用HTML 5画布仅绘制图像的圆形部分?

例如,拍摄照片并仅在画布内绘制脸部.

Var*_*ant 6

实现此目的的方法是使用clip定义剪切区域:

var ctx;
var canvas;

var img = new Image();

img.onload = function() {

    canvas = document.getElementById('myCanvas');
    ctx = canvas.getContext('2d');


    ctx.beginPath();
    ctx.arc(100, 100, 60, 0, 6.28, false); //draw the circle
    ctx.clip(); //call the clip method so the next render is clipped in last path
    ctx.stroke();
    ctx.closePath();
    ctx.drawImage(img, -190, 0);
};

img.src = "http://www.antiquiet.com/wp-content/uploads/2011/03/Free-Trapper_Remasters_The-Kills-467x311.jpg";
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到一个工作小提琴:http://jsfiddle.net/hJS5B/47/

从我对这个问题的回答中重复使用的代码:绘制多个带有图像内容的圆圈