HTML5 Canvas - заполнить круг с изображением - PullRequest
28 голосов
/ 25 ноября 2010

Как я могу нарисовать изображение внутри круга? Если я сделаю:

context.beginPath();
context.arc((e.pageX),(e.pageY),161,0,Math.PI*2,true);
context.closePath();

Как я могу использовать fill (), чтобы заполнить его нарисованным изображением?

Ответы [ 5 ]

51 голосов
/ 31 июля 2011

Я сделал это на днях для большого дела, которое я делаю;

var thumbImg = document.createElement('img');

thumbImg.src = 'path_to_image';
thumbImg.onload = function() {
    tmpCtx.save();
    tmpCtx.beginPath();
    tmpCtx.arc(25, 25, 25, 0, Math.PI * 2, true);
    tmpCtx.closePath();
    tmpCtx.clip();

    tmpCtx.drawImage(thumbImg, 0, 0, 50, 50);

    tmpCtx.beginPath();
    tmpCtx.arc(0, 0, 25, 0, Math.PI * 2, true);
    tmpCtx.clip();
    tmpCtx.closePath();
    tmpCtx.restore();
};

Работало идеально для меня.

Вот более сложная версия, которую я сделал, которая также выполняет кэширование изображений, https://jsfiddle.net/jaredwilli/ex5n5/

10 голосов
/ 19 января 2011

Не уверен, если вы все еще ищете ответ, но вот как:

var ctx = document.getElementById('your_canvas').getContext("2d");
//ctx.lineWidth = 13; 
//ctx.strokeStyle = 'rgba(0,0,0,1)'; 
//ctx.fillStyle="rgba(0,0,0,0)" // if using this, make sure alpha < 1

ctx.arc(100,100, 50, 0, Math.PI*2,true); // you can use any shape
ctx.clip();

var img = new Image();
img.addEventListener('load', function(e) {
    ctx.drawImage(this, 0, 0, 200, 300);
    //ctx.fill();
//ctx.stroke();
}, true);
img.src="/path/to/image.jpg";

Вы также можете сделать это с рисунком, но вы получаете меньшую гибкость размещения изображения

ctx.arc(100,100, 70, 0, Math.PI*2,true);
ctx.clip();

img = new Image()
img.addEventListener('load', function(e) {
    ctx.fillStyle = ctx.createPattern(this, 'no-repeat') 
    ctx.fill();
}, true);
img.src="/path/to/image.jpg"
4 голосов
/ 25 ноября 2010

Рассмотрите возможность использования некоторых из этих альтернатив:

  • Использование <img> с CSS для border-radius: http://jsfiddle.net/ChrisMorgan/BQGxA/

  • Используйте SVG вместо<canvas> и установите эллипс как обтравочный контур для изображения.(Более сложные пути отсечения тоже просты)

Не зная больше о ваших требованиях и ситуации, я не знаю, удовлетворят ли они вашим требованиям, но я думаю, что они того стоятпри рассмотрении.<canvas> - это не решение всех ваших проблем - для многих из этих случаев CSS в обычном HMTL и / или SVG может быть более подходящим.

3 голосов
/ 27 августа 2014

Проблема с методом clip () заключается в том, что Chrome будет отображать границы без сглаживания, как показано в этом вопросе .

Одним из решений является использование globalCompositeOperation, как показано в ответе Дэниела:

//set-up - probably only needs to be done once
var scratchCanvas = document.createElement('canvas');
scratchCanvas.width = 100;
scratchCanvas.height = 100;
var scratchCtx = scratchCanvas.getContext('2d');


//drawing code
scratchCtx.clearRect(0, 0, scratchCanvas.width, scratchCanvas.height);

scratchCtx.globalCompositeOperation = 'source-over'; //default

//Do whatever drawing you want. In your case, draw your image.
scratchCtx.drawImage(imageToCrop, ...);


//As long as we can represent our clipping region as a single path, 
//we can perform our clipping by using a non-default composite operation.
//You can think of destination-in as "write alpha". It will not touch
//the color channel of the canvas, but will replace the alpha channel.
//(Actually, it will multiply the already drawn alpha with the alpha
//currently being drawn - meaning that things look good where two anti-
//aliased pixels overlap.)
//
//If you can't represent the clipping region as a single path, you can
//always draw your clip shape into yet another scratch canvas.

scratchCtx.fillStyle = '#fff'; //color doesn't matter, but we want full opacity
scratchCtx.globalCompositeOperation = 'destination-in';
scratchCtx.beginPath();
scratchCtx.arc(50, 50, 50, 0, 2 * Math.PI, true);
scratchCtx.closePath();
scratchCtx.fill();


//Now that we have a nice, cropped image, we can draw it in our
//actual canvas. We can even draw it over top existing pixels, and
//everything will look great!

ctx.drawImage(scratchCanves, ...);
0 голосов
/ 25 ноября 2010
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...