canvas.on('object:over', function(e) {
//I want change opacity of object on mouse over on object
});
canvas.on('object:out', function(e) {
});
I want opacity one when object:over and otherwise opacity should be 0
If the context is 2d. Using context.globalAlpha will solve the case.
<script>
window.onload = function() {
var canvas=document.getElementById("drawing"); // grabs the canvas element
var context=canvas.getContext("2d"); // returns the 2d context object
canvas.on('object:over', function(e) {
//I want change opacity of object on mouse over on object
//blue rectangle
context.fillStyle= "#b0c2f7";
context.globalAlpha=0; // Half opacity
context.fillRect(10,10,100,50);
});
canvas.on('object:out', function(e) {
context.fillStyle= "#b0c2f7";
context.globalAlpha=1; // full opacity
context.fillRect(10,10,100,50);
});
}
</script>
I am export of fabric.js. But the canvas in fabric.js haven't object:over and object:out.
fabric.js have mouse:over, mouse:out, object:moving: doc address http://fabricjs.com/docs/fabric.Canvas.html
when mouse:over
canvas.on('mouse:over', function(e) {
var targetObj = e.target;
targetObj.setOpacity(0.5);
});
canvas.on('mouse:out', function(e) {
var targetObj = e.target;
targetObj.setOpacity(1);
});
Related
I am using fabric js version 1.7.22
when image set in a repetitive manner in a rectangle of fabric js, at
the first time it will be loaded and saved into JSON using toJSON()
and save an image using todataUrl() method, but when cal canvas a loadFromJson method at that time, this canvas not savable, because it throws tainted canvas error.
Please help me,
I already set crossOrigin in a pattern but it not working. and not
added in canvas JSON.
I have made one Fiddle For Generate Issue :
[http://jsfiddle.net/Mark_1998/kt387vLc/1/][1]
Steps to generate issue :
click on 'set pattern'
then click on 'save canvas'
then click on 'reload canvas' // load canvas from JSON
then click on 'save canvas' // cause issue of tainted canvas
This issue is fixed in new version of fabricjs already. If you are still using 1.7.20 the override fabric.Pattern.prototype.toObject and fabric.Pattern.prototype.initialize, find code in snippet.
var canvas = new fabric.Canvas('canvas', {
height: 500,
width: 500,
});
canvas.backgroundColor = '#ff0000';
canvas.renderAll();
var canvasJSON = {};
document.getElementById('setPat').addEventListener('click', function() {
fabric.util.loadImage('https://cdn.dribbble.com/assets/icon-backtotop-1b04df73090f6b0f3192a3b71874ca3b3cc19dff16adc6cf365cd0c75897f6c0.png', function(image) {
var pattern = new fabric.Pattern({
source: image,
repeat: 'repeat',
crossOrigin: 'Anonymous'
});
var patternObject = new fabric.Rect({
left: 0,
top: 0,
height: canvas.height,
width: canvas.width,
angle: 0,
fill: pattern,
objectCaching: false
})
canvas.add(patternObject);
}, null, {
crossOrigin: 'Anonymous'
});
})
document.getElementById('saveCanvas').addEventListener('click', function() {
console.log('save canvas');
canvasJSON = canvas.toJSON();
var image = canvas.toDataURL("image/png", {
crossOrigin: 'Anonymous'
}); // don't remove this, i need it as thumbnail.
//console.log('canvas.Json', canvasJSON);
//console.log('image', image);
canvas.clear();
canvas.backgroundColor = '#ff0000';
canvas.renderAll();
});
document.getElementById('reloadCanvas').addEventListener('click', function() {
console.log('save canvas');
canvas.loadFromJSON(canvasJSON, function() {
canvas.set({
crossOrigin: 'Anonymous'
})
});
console.log('canvas.Json', canvasJSON);
});
//cross origin was not added in toObject JSON
fabric.Pattern.prototype.toObject = (function(toObject) {
return function() {
return fabric.util.object.extend(toObject.call(this), {
crossOrigin: this.crossOrigin,
patternTransform: this.patternTransform ? this.patternTransform.concat() : null
});
};
})(fabric.Pattern.prototype.toObject);
//cross origin was not added while creating image
fabric.Pattern.prototype.initialize = function(options, callback) {
options || (options = {});
this.id = fabric.Object.__uid++;
this.setOptions(options);
if (!options.source || (options.source && typeof options.source !== 'string')) {
callback && callback(this);
return;
}
// function string
if (typeof fabric.util.getFunctionBody(options.source) !== 'undefined') {
this.source = new Function(fabric.util.getFunctionBody(options.source));
callback && callback(this);
} else {
// img src string
var _this = this;
this.source = fabric.util.createImage();
fabric.util.loadImage(options.source, function(img) {
_this.source = img;
callback && callback(_this);
}, null, this.crossOrigin);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.20/fabric.js"></script>
<button id="setPat">
Set pattern
</button>
<button id="saveCanvas">
Save canvas
</button>
<button id="reloadCanvas">
Reload CAnvas
</button>
<canvas id="canvas"></canvas>
I am using Fabric.js to draw rectangles on a canvas. After drawing a rectangle, I want to disable all events on that object. I have tried to do it using canvas.__eventListeners["mouse:down"] = []; but then after the object selection is cleared, the canvas can't apply any events.
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>HTML5 canvas - Image color picker | Script Tutorials</title>
<link href="index.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.1.0/fabric.all.min.js" ></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="column1">
<canvas id="panel" width="700" height="350"></canvas>
</div>
<div style="clear:both;"></div>
<div id="log"></div>
</div>
</body>
<script>
(function() {
var canvas = new fabric.Canvas('panel');
var clicks=0;
var x1=0;var y1=0;
var rect;
canvas.on('mouse:down', function(e){
//check if you clicked on an object that exists canvas
if(e.target == undefined){
if (clicks == 0) {
var pointer=canvas.getPointer(event.e);
console.log(pointer);
x1 = pointer.x;
y1 =pointer.y;
console.log("Start Pointer " +x1 ,y1);
clicks++;
}
else
{
var endpointer=canvas.getPointer();
console.log(endpointer);
var endx=endpointer.x;
var endy=endpointer.y;
console.log("Endpointer " +endx ,endy);
console.log("x and y"+x1,y1);
var newwidth=endpointer.x- x1;
var newheight=endpointer.y - y1;
rect=new fabric.Rect({
left:x1,
top: y1,
originX :'left',
originY :'top',
width:newwidth,
height:newheight,
selectable: true,
evented:false,
fill:'red',
opacity :0.3
});
canvas.add(rect);
//console.log(rect.setWidth(pointer2.x- x1 ));
//console.log(rect.setHeight( pointer2.y - y1));
canvas.renderAll();
clicks=0;
}
}
else
{
//canvas.getActiveObject().remove();
canvas.__eventListeners["mouse:down"] = [];
}
});
canvas.on('object:moving',function(){
var bound=rect.getBoundingRect();
console.log(bound.left,bound.top,bound.width,bound.height);
});
canvas.on('object:scaling',function(){
var bound=rect.getBoundingRect();
console.log(bound.left,bound.top,bound.width,bound.height);
});
canvas.on('object:selected',function(e){
document.onkeydown = function(e) {
if (e.keyCode === 27||e.button==3) {
e.preventDefault();
canvas.getActiveObject().remove();
}
}
});
fabric.Image.fromURL('fedex.jpg', function (img) {
canvas.add(img.set({
width: 700,
height:350,
left: 350,
top: 175,
selectable: false,
}));
});
})();
</script>
With the method canvas.off('mouse:down', eventHandler) event handlers registered using canvas.on('mouse:down', eventHandler) can be removed. Be sure to pass the same function into both calls (e.g. by assigning the handler to a variable).
This method is available on all Observables in fabric (see API doc: http://fabricjs.com/docs/fabric.Observable.html#off)
What is the final answer? We're basing our decision to mousedown on or mousedown off, based on the event - if it has a target. I came up with this solution (a 2nd mousedown handler), but it didn't work.
canvas.on('mouse:down', function (e) {
// canvas click, not object click; so create the obj
if (e.target === undefined) {
canvas.on('mouse:down', eventHandler);
}
else {
// an object was clicked, so don't create the obj again
canvas.off('mouse:down', eventHandler);
}
});
Where eventHandler is the function that handles object creation.
I have this code below that helps me align the image to the center on any device/screen.
Now I need help with drawing the picture(myImage) as canvas and not $("body").append(img);
How can I do this?
<script>
$(document).ready(function() {
var img=new Image();
img.src="myImage.jpg"
img.onload=function() {
// console.log("image is loaded, yessss!")
function my_resize() {
var w=img.width;
var h=img.height;
var ww=$(window).width();
var wh=$(window).height();
var ox=(ww-w)/2;
var oy=(wh-h)/2;
console.log("data is "
+w+","+
+h+","+
+ww+","+
+wh+","+
+ox+","+
+oy+","
);
$(img).offset({left:ox,top:oy,});
}
$("body").append(img);
my_resize();
$(window).resize(function() {
my_resize();
});
}
});
</script>
Try this jsfiddle:
http://jsfiddle.net/MqkRs/4/
I'm creating an interactive app allowing people to customise doors. To allow them to select the letterbox I want to show it when they hover over the door and remove it when they hover out. This works fine, but when I hover the letter box the doors 'hover out' is fired.
This causes a strange flickering effect.
I have created a jsfiddle here showing this effect
Just wondering if anyone has a solution to this?
I basically need the letterbox to stay in place when the user hovers the door, I also need a click state for both the door and letterbox.
not sure if this is the most efficient method, but it works. jsfiddle
var doorClickState, letterbox, inletterbox = false;
$(function() {
var paper = Raphael("canvas", 330, 457);
//draw the door
doorClickState = paper.path("M0,0v200h117V0H0z").translate(0, 0).attr({
fill: "#FF0000",
stroke: 0,
opacity: 0.9
}).toFront();
//draw and hide letterbox
letterbox = paper.path("M0,0v15h60V0H0z").translate(30, 100).attr({
fill: "#000000",
stroke: 0,
opacity: 0.9
}).hide();
//click states for both
doorClickState.click(function() {
alert('door clicked');
});
letterbox.click(function() {
alert('letterbox clicked');
});
doorClickState[0].onmouseover = function() {
letterbox.show();
}
letterbox[0].onmouseover = function() {
inletterbox = true;
}
letterbox[0].onmouseout = function() {
inletterbox = false;
}
doorClickState[0].onmouseout = function() {
setTimeout(function() {
if (!inletterbox) {
letterbox.hide();
}
}, 20);
};
});
anyone come across a code that only fire event when the mouse enter the element for certain time ? but won't fire event if only hover or passed thru the element quickly..
Using setTimeout, it's not the MooTools way. What you should use is the framework's methods:
var theDiv = $$('div')[0];
var foo = function(){
theDiv.highlight();
};
var timer;
theDiv.addEvents({
mouseenter: function() {
timer = foo.delay(1000);
},
mouseleave: function() {
$clear(timer);
}
});
See a working example: http://www.jsfiddle.net/oskar/SZsNT/
var timer = null;
element.addEvents({
mouseenter: function() {
timer = setTimeout(foo, 5000);
},
mouseleave: function() {
clearTimeout(timer);
}
});
So foo will be called only if cursor was over element for 5 seconds