- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 easelJS 上编写位图橡皮擦。我想,我已经完成了一半的路....
这是演示: http://jsfiddle.net/bordeux/g2Lwvsuv/2/
和我的代码:
var example = function() {
this.constructor.apply(this, arguments);
};
example.prototype.constructor = function() {
this.$canvas = $("canvas");
this.container = {};
this.objects = {};
this.resizeEvents = [];
this.prepareCanvas();
this.refreshSize();
this.bindEvents();
};
example.prototype.bindEvents = function() {
var self = this;
$(window).resize(function(){
self.refreshSize();
});
};
example.prototype.refreshSize = function() {
this.stage.canvas.width = window.innerWidth;
this.stage.canvas.height = window.innerHeight;
this.resizeEvents.forEach(function(item) {
item();
});
};
example.prototype.getObject = function(name) {
return this.objects[name];
};
example.prototype.setObject = function(name, obj) {
this.objects[name] = obj;
return obj;
};
example.prototype.addResizeEvent = function(foo) {
this.resizeEvents.push(foo);
return this;
};
example.prototype.initCursor = function() {
var self = this;
var size = 50;
/**
* Circle cursor
*/
var circle = new createjs.Shape();
circle.graphics.setStrokeStyle(2).beginStroke("#171717").drawCircle(0, 0, size);
/**
* Hit area
*/
var hit = new createjs.Shape();
hit.graphics.beginFill("#000").drawCircle(0, 0, size);
circle.hitArea = hit;
var container = this.getContainer("cursor");
container.addChild(circle);
this.stage.on("stagemousemove", function(evt) {
circle.setTransform(evt.stageX, evt.stageY);
this.setChildIndex( container, this.getNumChildren()-1);
});
var drawing = this.setObject("image.mask", new createjs.Shape());
drawing.visible = false;
var lastPoint = new createjs.Point();
container.addChild(drawing);
circle.on("mousedown", function(event) {
lastPoint.x = event.stageX;
lastPoint.y = event.stageY;
});
circle.on("pressmove", function(event) {
drawing.graphics
.setStrokeStyle(100, "round", "round")
.beginStroke("rgba(255, 255, 255, 0.2)")
.beginRadialGradientFill(
["rgba(0,0,0,0)", "rgba(0,0,0,1)"],
[0.1, 1],
50, 50, 0,
50, 50, 50
)
.moveTo(lastPoint.x, lastPoint.y)
.lt(event.stageX, event.stageY);
lastPoint.x = event.stageX;
lastPoint.y = event.stageY;
self.updateCache();
});
circle.on("pressup", function(event) {
self.updateCache();
});
};
example.prototype.updateCache = function(update) {
(update === undefined) && (update = false);
var drawingCanvas = this.getObject("image.mask");
var image = this.getObject("image");
if (update) {
drawingCanvas.updateCache();
} else {
drawingCanvas.cache(0, 0, image.image.naturalWidth, image.image.naturalHeight);
}
image.filters = [
new createjs.AlphaMaskFilter(drawingCanvas.cacheCanvas)
];
if (update) {
image.updateCache(0, 0, image.image.naturalWidth, image.image.naturalHeight);
} else {
image.cache(0, 0, image.image.naturalWidth, image.image.naturalHeight);
}
};
example.prototype.prepareCanvas = function() {
this.stage = new createjs.Stage(this.$canvas[0]);
createjs.Ticker.setFPS(60);
createjs.Ticker.addEventListener("tick", this.stage);
this.initCursor();
this.loadImage();
};
/**
* Get container. If not exist, this function create new one
* @param {String} name
* @returns {createjs.Container}
*/
example.prototype.getContainer = function(name) {
if(this.container[name]){
return this.container[name];
}
this.container[name] = new createjs.Container();
this.stage.addChild(this.container[name]);
return this.container[name];
};
example.prototype.loadImage = function() {
var self = this;
var url = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Logo_Google_2013_Official.svg/1280px-Logo_Google_2013_Official.svg.png";
var background = this.setObject("image.background", new createjs.Shape());
this.getContainer("image").addChild(background);
var image = null;
return utils.image(url).then(function(img){
image = img;
self.getContainer("image").addChild(self.setObject(
"image",
new createjs.Bitmap(img)
));
self.updateCache(false);
return utils.image("http://i.imgur.com/JKaeYwv.png");
}).then(function(imgBackground){
background
.graphics
.beginBitmapFill(imgBackground,'repeat')
.drawRect(0,0, image.naturalWidth, image.naturalHeight);
});
};
utils = {};
utils.image = function(url){
var deferred = Q.defer();
//deferred.resolve(text);
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
deferred.resolve(this);
};
img.onerror = function(){
deferred.reject(arguments);
};
img.src = url;
return deferred.promise;
};
$(function(){
var test = new example();
});
但是当我在我的 Canvas 上画东西时,我的绘制路径显示了 mask 后面的图像......这与我想要的结果相反。
我想删除部分图像(如 Photoshop 中的橡皮擦)。我不知道我接下来应该做什么......
谢谢
最佳答案
您可以将 compositeOperation
与层或缓存结合使用以“删除”内容。例如,如果您使用 updateCache("source-over")
,它将在现有缓存上绘制当前视觉内容(正常合成)。但是,如果您使用 updateCache("destination-out")
,这实际上会从现有缓存中删除当前内容,从而产生橡皮擦效果。
这是一个简单的例子:http://jsfiddle.net/17xec9y5/4
关于javascript - 在 EaselJs 中创建橡皮擦 - 下一步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31853911/
我正在尝试水平翻转 SpriteSheet 动画中的 Sprite 。我从this example开始 我在创建SpriteSheet后立即添加了翻转的帧 var ss = new creat
我正在尝试水平翻转 SpriteSheet 动画中的 Sprite 。我从 this example 开始 我在创建 SpriteSheet 后立即添加了翻转帧 var ss = new cr
是否可以使透明rect可点击? 我不想填充它,所以看起来像 shape.graphics.setStrokeStyle(2).beginStroke("#000").rect(0, 0, 1
如何在createJS中绘制圆弧。我已经经历过 arcTo 函数,但这不是我想要的。我希望能够绘制几条弧线,这些弧线放在一起时类似于一个圆。例如:我希望能够使用 8 条弧线绘制一个圆。无法使用 Arc
当我尝试使用时: temp = new createjs.Bitmap(obj.path) 我收到错误: Uncaught An error has occurred. This is most li
我正在用 EaselJS 制作一个旋转矩形,但它不像我想的那样工作。 我认为,如果我想制作一个围绕其中心在 x=100, y=100 位置旋转的正方形 (40x40),我需要将它的注册点设置为 reg
我在使用 EaselJS 时遇到了困难。基本上我想创建一个简单的网格并突出显示所选的实际元素: var stageWidth = 800, stageHeight = 600, cell_
我最近发现了EaselJS,我正在尝试制作一个非常简单的 Canvas 射击游戏。 我的 Canvas 宽度为 500 像素,高度为 500 像素。 当用户的鼠标进入 Canvas 时,目标会随着鼠标
我正在使用 EaselJS SpriteSheets,我想知道如何让我的英雄停止平稳运行,因此,如果 SpriteSheet 正在播放“运行”动画并且位于第 5 帧,我需要通过帧进行动画处理6, 7,
我的形状要么从地板上掉下来,要么消失。我的代码很简单,如果您复制并粘贴,您就可以看到所有内容。无论如何,听到的是代码。我想我可能没有在tick函数中的碰撞中添加一些东西。 var box, gravi
EaselJS.js 库有很多我的项目中不需要的函数(我不调用它们)。有没有办法检测这些功能并为每个项目删除它们?我需要尽可能减小文件大小。 附注我的easeljs.js是在Adobe Flash C
我正在尝试提高 Canvas 上的性能,因为每个 stage.update() 上都会重新绘制所有内容(多个区域),这就是它的工作原理。我只想绘制/添加一个区域。我可以让它工作,但是由于更新,所有以前
我正在使用 EaselJS 用 JS 编写一个简单的游戏。我尝试保持一切面向对象,以保持游戏状态、EaselJS 对象状态以及 Canvas 上显示的内容同步。我希望能够通过更改属性来更改 Canva
我希望我的位图转到单击位置,但我希望看到 bitmap.x/bitmap.y 和 click.x/click.y 之间的进展我怎样才能制作这个动画? 非常感谢 最佳答案 单击舞台时,您可以使用 Twe
我需要使用 Shape 对象创建可拖动的随机矩形。此函数在单击鼠标的位置创建新的矩形。 var rects = []; var i=0; var stage; window.onload = func
在代码的开头,我设置了一个刻度事件。以下 3 行有效。 createjs.Ticker.addEventListener("tick", handleTick); createjs.Ticker.us
我正在尝试在 Canvas 中创建可拖动的背景图像。目前我有这样的东西: var stage = new createjs.Stage("canvas"); createjs.Ticker.addEv
所以我正在使用 easeljs,并且我有一堆要回收的图像。由于某种原因,每当我尝试将它们添加到舞台 stage.update() 就不再起作用。 (我使用警报来测试这一点,因此它可能会变得非常慢例如加
我刚开始使用 easeljs 库。我创建了一个带舞台的 Canvas ,它可以正常工作。 我想添加位图作为按钮并将它们并排添加到舞台底部。 我试过以下方法,但它会将位图放在彼此的顶部。 var but
我刚开始通过在线教程学习 javascript,并从 GitHub 存储库下载文件。文件类型为“easeljs-0.7.1.min”。当我输入下面给出的代码时,它没有显示任何内容在浏览器上。是我下载的
我是一名优秀的程序员,十分优秀!