LoginSignup
2
2

More than 5 years have passed since last update.

canvasでつくる円形のくり抜きレイヤー

Last updated at Posted at 2017-02-28

こういう円形のくり抜きレイヤーをcanvasで気軽に描画する。
スクリーンショット 2017-02-28 23.49.30.png

HTML
<canvas id="image" style="height: 300px; width: 300px;" height="300" width="300"></canvas>
JavaScript
var image = new Image()

image.onload = function() {
  canvas = document.getElementById("image");
  context = canvas.getContext("2d");

  context.fillStyle = "rgba(0, 0, 0, 0.5)";
  context.fillRect(0, 0, 300, 300);

  context.fillStyle = "rgb(255, 255, 255)";
  context.globalCompositeOperation = "destination-out";
  context.arc(150, 150, 150, 0, Math.PI * 2);
  context.fill();

  context.globalCompositeOperation = "destination-over";
  context.drawImage(image, 0, 0, 300, 300)
}

image.src = "https://frenchmoments.eu/wp-content/gallery/nancy-place-stanislas/copyright-french-moments-place-stanislas-nancy-8.jpg"
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2