EaselJS图像创建为圆形

时间:2016-03-17 18:54:09

标签: canvas html5-canvas createjs easeljs

我在EaselJS中努力制作图像变得圆润。

基本上,我在EaselJS中寻找与border-radius相当的CSS3并且无法找到解决方案。

我最接近的就是创建一个圆圈形状,并为其添加BitmapFill ......我看到的问题是BitmapFill被卡住了在圆形状后面的(0, 0) x / y位置,当我将形状移动到图像大小之外的任何位置时,Bitmap不会跟随(或快照)到形状并随之移动。

检查我的Fiddle here

如果您将圆 x位置更改为50,它看起来很正常......但请注意如何将圆圈远离(50, 50) x / y位置Bitmap落后而不是跟随。

请参阅下文,了解我的HTML / CSS / Javascript在小提琴中的外观:

HTML:

<!-- CreateJS CDN -->
<script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>
<canvas id="stage-canvas" width="300" height="300">
  This web browser does not support canvas.
</canvas>

CSS:

canvas {
  background-color: #FFF;
  display: block;
}

使用Javascript:

// Self running function (once page loads)
(function(){
  // preload image assets first with preloadJS
  // used from example here: http://createjs.com/docs/preloadjs/modules/PreloadJS.html
  var queue = new createjs.LoadQueue();
  queue.on("complete", loaded, this);
  queue.loadManifest([
     {id: "poke", src:"https://upload.wikimedia.org/wikipedia/en/f/f1/Bulbasaur_pokemon_red.png"}
  ]);

  // once images above preloaded, run this function:
  function loaded() {
    // Init Stage
    var stage = new createjs.Stage("stage-canvas")

    // Create Background
    var bg1 = new createjs.Shape()
    bg1.graphics.beginFill("ghostwhite") // first bg is white
    bg1.graphics.drawRect(
      0,                    // x position
      0,                    // y position
      stage.canvas.width,   // width of shape (in px)
      stage.canvas.height   // height of shape (in px)
    )
    // Can only define this after shape is drawn, else no fill applies
    bg1.graphics.ef()    // short for endFill()
    stage.addChild(bg1)  // Add Child to Stage

    // trying to create image to be circlular
    // this will add it as the background-image to a circle
    // but I'm having problems getting it to "snap" to the circle
    // instead of stuck at (0, 0) x/y position behind circle...?
    var circle = new createjs.Shape()
    circle.graphics.beginBitmapFill(
      queue.getResult("poke"), // image to be used as fill
      "no-repeat" // image repeating or not
    )
    circle.graphics.drawCircle(
      150, // x position
      50, // y position
      50 // diameter (in px)
    )
    stage.addChild(circle)

    stage.update()
    }
})()

感谢您提前帮助我弄清楚如何修复我的代码,或者我是否在错误的路径上。

更新:我最初问这个问题是做圆形图像还是带圆角的图像。我真的只需要圆形图像方法,并且由于提供的答案仅针对此,我已经删除了我的问题,还要求圆角解决方案。

3 个答案:

答案 0 :(得分:1)

您可以使用mask上的Bitmap属性(http://www.createjs.com/docs/easeljs/classes/Bitmap.html#property_mask)来获得圆角。

var stage = new createjs.Stage(document.getElementById('canvas'));

var img = new Image();
img.onload = function() {
    var container = new createjs.Container();

    var bitmap = new createjs.Bitmap(img);
    bitmap.regX = bitmap.regY = 150;
    bitmap.y = bitmap.x = 150;

    var maskShape = new createjs.Shape(new createjs.Graphics().f("#000").drawCircle(150,150,150));
    bitmap.mask = maskShape;

    container.addChild(bitmap);

    stage.addChild(container);
    stage.update();

    container.x = container.y = 50;

    stage.update();
};
img.src = '//unsplash.it/300/300';

https://jsfiddle.net/2tsym49r/3/

修改: 正如Lanny所提到的mask使用Canvas clip方法,因此不会在Chrome中使用抗锯齿(open bug)。如果您需要在Chrome中使用抗锯齿功能,可以使用compositeOperation进行解决方法,请查看更新的jsfiddle:https://jsfiddle.net/2tsym49r/4/

答案 1 :(得分:1)

我建议将圆圈居中,并使用矩阵也使位图填充居中。

// Create a matrix
var matrix = new createjs.Matrix2D()
    .translate(-160/2, -144/2); // Move to 50% the width and height of the image

circle.graphics.beginBitmapFill(
  queue.getResult("poke"), // image to be used as fill
  "no-repeat",
  matrix // Pass in the matrix
);

// Draw the circle at 0,0
circle.graphics.drawCircle(
  0, // x position
  0, // y position
  50 // diameter (in px)
);

// Move it to wherever you want
circle.x = 100;
circle.y = 100;

这是一个更新的小提琴:https://jsfiddle.net/lannymcnie/md73ns53/

您还可以缩放矩阵以使图像适合圆圈。

答案 2 :(得分:0)

将圆形形状放入容器中,然后将容器移动到所需位置。这是我发现的唯一选择:

See this Fiddle

更改此内容:

var circle = new createjs.Shape()
circle.graphics.beginBitmapFill(
  queue.getResult("poke"), // image to be used as fill
  "no-repeat" // image repeating or not
)
circle.graphics.drawCircle(
  150, // x position
  50, // y position
  50 // diameter (in px)
)
stage.addChild(circle)

对此:

var circle = new createjs.Shape()
circle.graphics.beginBitmapFill(
  queue.getResult("poke"), // image to be used as fill
  "no-repeat" // image repeating or not
)
circle.graphics.drawCircle(
  50, // x position
  50, // y position
  50 // diameter (in px)
)
// Create a container for the circle
var container = new createjs.Container()
container.x = 150
container.y = 150
container.addChild(circle) // add circle to container
stage.addChild(container) // then add container to stage