【发布时间】: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