我正在努力使EaselJS中的圖像變爲圓形。EaselJS將圖像創建爲圓形
基本上,我正在尋找EaselJS中的border-radius
的CSS3等價物,並且找不到解決方案。
我得到的最接近的是創建一個圓形狀,並添加一個BitmapFill
它...我看到的問題是BitmapFill
被卡在一個(0, 0)
x/y位置後面的圓Shape ,並且當我將Shape移動到圖像大小以外的任何位置時,Bitmap
不會跟隨(或管理單元)並隨之移動。
檢查我Fiddle here
如果你改變了圈x位置到50
,它看起來normal'ish ......但要注意如何從(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()
}
})()
預先感謝您幫助我弄清楚如何修復我的代碼,或者如果我在這裏錯誤的路徑。
更新:我原來問過這個問題做一個圓的圖像,或帶有圓角的圖像。我真的只需要圓形圖像方法,因爲提供的答案只針對這一點,所以我刪除了我的問題,並要求提供圓角解決方案。
面具是偉大的(也是最高性能),但請注意,它不會在Chrome中進行抗鋸齒。位圖填充方法解決了這個問題。 – Lanny
我真的更喜歡這種面具方法,但這仍然會導致我嘗試移動x/y位置時遇到的相同問題:[jsfiddle示例](https://jsfiddle.net/skplunkerin/2tsym49r/2/)。任何想法如何也解決@derz? – skplunkerin
也@derz,你的jsfiddle需要更新,以符合你的代碼:)(也可以添加createjs腳本包括所以它的工作原理) – skplunkerin