【问题标题】:EaselJS Image created as circleEaselJS 图像创建为圆形
【发布时间】:2016-07-04 07:39:51
【问题描述】:

我正在努力使 EaselJS 中的图像变成圆形。

基本上,我正在 EaselJS 中寻找与 border-radius 等效的 CSS3,但找不到解决方案。

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


查看我的Fiddle here

如果您将圆的x 位置 更改为50,它看起来很正常...但是请注意将圆从(50, 50) x/y 位置移动得更远,@987654329 @ 被抛在后面而不是跟随。

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

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()
    }
})()

提前感谢您帮助我弄清楚如何修复我的代码,或者如果我在这里走错了路。


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

【问题讨论】:

    标签: canvas html5-canvas createjs easeljs


    【解决方案1】:

    您可以在Bitmap 上使用mask 属性(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 中进行抗锯齿(打开错误)。如果您需要在 Chrome 中进行抗锯齿,您可以使用复合操作来解决问题,检查更新的 jsfiddle:https://jsfiddle.net/2tsym49r/4/

    【讨论】:

    • Mask 很棒(也是性能最高的),但请注意它不会在 Chrome 中进行抗锯齿处理。位图填充方法解决了这个问题。
    • 我真的更喜欢这种掩码方法,但这仍然会导致我在尝试移动 x/y 位置时遇到的相同问题:jsfiddle example。知道如何修复@derz 吗?
    • 还有@derz,您的 jsfiddle 需要更新以匹配您上面的代码:)(还添加 createjs 脚本以使其工作)
    • 我已经更新了你的小提琴(并更正了链接;-))。 jsfiddle.net/2tsym49r/3 但是我不确定,为什么掩码与位图无关——我认为应该如此。 @Lanny 我错了吗?
    • 还有另一个在 Chrome 中获得抗锯齿的更新:jsfiddle.net/2tsym49r/4 但是我在这里也不确定,什么更快 - 使用复合操作解决方法或 bitmapFill。也许@Lanny 做了一些性能测试?
    【解决方案2】:

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

    // 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/

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

    【讨论】:

    • 谢谢@Lanny,我正试图了解矩阵在这里为我们做了什么?你能解释一下吗? (我的数学很差:P)我不明白“只是解决这个问题”?
    • 矩阵只是告诉填充变换一个特定的量(缩放、旋转、平移)。由于您只想移动(平移)坐标,因此我使用了translate 方法,将其移动到图像宽度和高度的一半(-w/2、-h/2)。希望对您有所帮助。
    【解决方案3】:

    将圆形形状放入容器中,然后将容器移动到您想要的位置。这是目前我找到的唯一选择:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-16
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2012-12-13
      • 2014-12-25
      • 2018-11-29
      相关资源
      最近更新 更多