Draw multiple Rectangles with svg.draw.js - javascript

The issue was already posted here, but I think was never addressed. I need to create a new rect each time I click "Create SVG Rect" button. Hence drawing multiple rectangles.
<!DOCTYPE html>
<html lang="en">
<head><style>#test_slide { width: 500px; height: 500px; cursor: crosshair;border: solid;}</style></head><body>
<div id="test_slide"></div>
<button id="create_svg_rect" >Create SVG Rect</button>
<!--SCRIPT FILES-->
<script src="https://code.jquery.com/jquery-3.3.1.min.js" defer="defer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.7.1/svg.min.js" type="text/javascript" defer="defer"></script>
<script src="https://svgjs.dev/svg.draw.js/demo/svg.draw.min.js" type="text/javascript" defer="defer"></script>
<script>
document.getElementById("create_svg_rect").onclick = function() {
//SVG.on(document, 'DOMContentLoaded', function() {
var drawing = new SVG('test_slide').size('100%', '100%');
var rect = drawing.rect().attr('stroke-width',1).attr('fill','none');
drawing.on('mousedown', function(e){
rect.draw(e);
}, false);
drawing.on('mouseup', function(e){
rect.draw('stop', e);
console.log(rect.svg());
return;
}, false);
//});
};
</script>
</body></html>

In your code you were creating a whole new drawing and listeners everytime you click on the button. Thats not what you want.
Instead, just create one drawing (<svg>) and attach the listeners to it.
A click on the button only has to create a new rectangle.
To be able to access the rect-var in the listeners we have to define it in the outer scope. On click, we create a new rect and save it. On mousedown we start drawing and on mouseup we finish.
Here is working snippet:
document.addEventListener('DOMContentLoaded',() => {
let rect, drawing = new SVG('test_slide').size('100%', '100%');
drawing.on('mousedown', function(e){
rect && rect.draw(e);
});
drawing.on('mouseup', function(e){
if (rect) {
rect.draw('stop', e);
console.log(rect.svg());
rect = null;
}
});
document.getElementById("create_svg_rect").addEventListener('click', function() {
rect = drawing.rect().attr('stroke-width',1).attr('fill','none');
});
})
#test_slide { width: 500px; height: 160px; cursor: crosshair;border: solid;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.7.1/svg.min.js" type="text/javascript"></script>
<script src="https://svgjs.dev/svg.draw.js/demo/svg.draw.min.js" type="text/javascript"></script>
<div id="test_slide"></div>
<button id="create_svg_rect" >Create SVG Rect</button>
Since your code didnt use any jquery I removed it from the snippet.

Related

Detect if mouse is leaving browser window with button pressed

I'm using Kinetic.js for some dragging in my canvas and I'm trying to detect if the mouse leaves the browser window. Only difference is that I also want it to trigger when a mouse button is pressed while moving out.
This thread pretty much solves the question but if you press the left mouse button while moving out it doesn't work: Link
For me it seems that the mouseout event is being ignored as long as the left mouse button is pressed. I've done a test here. Any ideas?
You can set an isDown flag when the mouse is pressed.
Then clear the isDown flag when the mouse is released.
And track mouseout + the isDown flag to see if user is leaving with mouse pressed
Here's the jQuery version:
var isDown=false;
$(stage.getContent()).on('mousedown',function(e){ isDown=true; });
$(stage.getContent()).on('mouseup',function(e){ isDown=false; });
$(stage.getContent()).on('mouseout',function(e){
console.log(isDown);
isDown=false;
});
Here's code and a Fiddle: http://jsfiddle.net/m1erickson/ZjKGS/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Prototype</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.0.min.js"></script>
<style>
#container{
border:solid 1px #ccc;
margin-top: 10px;
width:400px;
height:400px;
}
</style>
<script>
$(function(){
var stage = new Kinetic.Stage({
container: 'container',
width: 300,
height: 300
});
var layer = new Kinetic.Layer();
stage.add(layer);
var isDown = false;
$(stage.getContent()).on('mousedown', function (e) {
isDown = true;
});
$(stage.getContent()).on('mouseup', function (e) {
isDown = true;
});
$(stage.getContent()).on('mouseout', function (e) {
if(isDown){
$("#indicator").text("Moved out and mouse was pressed");
}else{
$("#indicator").text("Moved out and mouse was not pressed");
}
isDown = false;
});
layer.draw();
}); // end $(function(){});
</script>
</head>
<body>
<p>Move mouse out of kinetic stage</p>
<p>Indicator will tell if mouse was also pressed</p>
<p id="indicator">Indicator</p>
<div id="container"></div>
</body>
</html>

How to implement Rotation and Drag&Drop in Kinetic.js?

I need a code to do the follow things: I've an image definited to Kinetic.Image, and this image should rotate; I have to use the drag and drop on this image, but the rotation doesn't allow the drag and drop. Can you give me a code to do this? I searched some tutorials but they don't help me. Thank you
KineticJS images are both draggable and rotatable already.
For the "drop", you can listen for the drop event on the image like this:
kImage.on('dragend', function() {
// do your drop tests here
});
Here is code and a Fiddle: http://jsfiddle.net/m1erickson/eCekf/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Prototype</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.4.min.js"></script>
<style>
#container{
border:solid 1px #ccc;
margin-top: 10px;
width:400px;
height:400px;
}
</style>
<script>
$(function(){
var stage = new Kinetic.Stage({
container: 'container',
width: 400,
height: 400
});
var layer = new Kinetic.Layer();
stage.add(layer);
var kImage;
var img=new Image();
img.onload=function(){
kImage=new Kinetic.Image({
image:img,
x:175,
y:175,
width:150,
height:150,
offset:[75,75],
draggable:true
});
layer.add(kImage);
kImage.rotate(30*Math.PI/180);
layer.draw();
}
img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house-icon.png";
$("#rotate").click(function(){
kImage.rotate(kImage.getRotation()+20*Math.PI/180);
layer.draw();
});
}); // end $(function(){});
</script>
</head>
<body>
<div id="container"></div>
<button id="rotate">Rotate</button>
</body>
</html>

Can I have a click and keyboard handler for RaphaelJS' canvas events?

I am new to RaphaelJS. I am trying to add click listener and keyboard listener to the canvas with no success. Can someone please explain how to use click listener and keyboard listener on Raphael. A small example will be of great help.
Thank you.
Here is a click and mouseover example, you can use more jQuery in there to simplify it but I just wanted to use the document ready function. Shouldn't be too much to add a keyboard event in there:
<html>
<head>
<script type="text/javascript" src="http://github.com/DmitryBaranovskiy/raphael/blob/master/raphael-min.js?raw=true"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
var paper = new Raphael(document.getElementById('canvas_container'), 500, 500);
var circ = paper.circle(250, 250, 40);
circ.node.onmouseover = function()
{
this.style.cursor = 'pointer';
};
circ.node.onclick = function()
{
circ.animate({opacity: 0}, 2000, function()
{
this.remove();
});
}
});
</script>
<style type="text/css">
#canvas_container
{
width: 500px;
border: 1px solid #aaa;
}
</style>
</head>
<body>
<div id="canvas_container"></div>
</body>
</html>

How can I get a css position change to take effect during a JQuery UI Draggable element's start event handler?

I have an odd situation in which I need to modify the position of a draggable element as soon as the user starts dragging it. So, during the draggable element's start event handler, I'm trying to set the position. It doesn't respond to the position change unless - and this is weird - I do something to cause a javascript error after I change the position. Here's an example:
<html>
<head>
<title>Drag reposition test</title>
<script type="text/javascript" src="js/css_browser_selector.js"></script>
<script type="text/javascript" src="development-bundle/jquery-1.3.2.js"></script>
<script type="text/javascript" src="development-bundle/ui/jquery-ui-1.7.1.custom.js"></script> <!-- Includes JQuery UI Draggable. -->
<style type="text/css">
#draggable { width: 150px; height: 150px; padding: 0.5em; }
</style>
<script type="text/javascript">
$(function() {
$("#initialdragger").draggable({
start: function(e, ui) {
$('#initialdragger').css('top', 400);
x = y; // Javascript error, which weirdly causes a redraw.
}
});
});
</script>
</head>
<body>
<div id="initialdragger" class="ui-widget-content" style="border-width: 1px; border-color: black; background-color: orange; width: 300px">
<p>Drag me around</p>
</div>
</body>
</html>
How can I legitimately cause a redraw to happen in this context? JQuery's hide() and show() don't work and neither do these methods.
I think binding a mousedown event will get you what you want
<script type="text/javascript">
$(function() {
$("#initialdragger").draggable();
$('#initialdragger').bind("mousedown", function(e) {
$(this).css('top', 400);
});
});
</script>

Javascript: Event does not fire unless element appended to document.body

I dynamically create an element (div) in javascript, on which i register an event listener:
var tooltip = document.createElement('div');
tooltip.onclick = function() { alert('hello'); }
Now, if I attach this element to the document body:
document.body.appendChild(tooltip);
all is well and the event is captured. However (for positioning purposes) i want to attach this element to a (static) sub-element within my page, e.g:
document.getElementById('id').appendChild(tooltip);
and the element is generated and positioned correctly - but the onclick event now is no longer captured. Any thoughts? This is x-browser, so i must be missing something.
Thanks, Don.
You're creating not only one but MANY divs.
Try this instead(I hope you don't mind but I fixed the HTML and CSS too):
<html>
<head>
<script type="text/javascript">
function makeDiv() {
if(!document.getElementById('tooltipDiv')){
var tooltip = document.createElement('div');
tooltip.id = "tooltipDiv";
// Give our tooltip a size and colour so we can see it
tooltip.style.height = '200px';
tooltip.style.position = 'absolute';
tooltip.style.width = '200px';
tooltip.style.backgroundColor = '#eee';
// Register onclick listener
tooltip.onclick = function() { alert('hello'); }
//tooltip.addEventListener("click", function(){ alert('hello'); }, false);
// *** Comment one of these out: ***
//document.body.appendChild(tooltip);
document.getElementById('myDiv').appendChild(tooltip);
}
}
</script>
</head>
<body>
<div id="myDiv"
onmouseover="makeDiv();"
style="position: relative; top: 100px; left: 100px; border: 1px solid red; width: 200px;">
<span>my div text</span>
</div>
</body>
</html>
Maybe you need to register the event handler after appending?
Your code works fine for me on firefox 3.0.5 and IE7. Are you sure your example is correct?
Ok all, here is my code, apologies for the delay. A version with a work-around is posted underneath:
<html>
<head>
<script type="text/javascript">
function makeDiv() {
var tooltip = document.createElement('div');
// Give our tooltip a size and colour so we can see it
tooltip.style.height = '200px';
tooltip.style.position = 'absolute';
tooltip.style.width = '200px';
tooltip.style.backgroundColor = '#eee';
// Register onclick listener
tooltip.onclick = function() { alert('hello'); }
// *** Comment one of these out: ***
//document.body.appendChild(tooltip);
document.getElementById('myDiv').appendChild(tooltip);
}
</script>
</head>
<body>
<div id="myDiv"
onmouseover="makeDiv();"
style="position: relative; top: 100px; left; 100px; border: 1px solid red; width: 200px;">
<span>my div text</span>
</div>
</body>
</html>
===================================
OK - so this works:
<html>
<head>
<script type="text/javascript">
function makeDiv() {
var tooltip = document.createElement('div');
// Give our tooltip a size and colour so we can see it
tooltip.style.height = '200px';
tooltip.style.position = 'absolute';
tooltip.style.width = '200px';
tooltip.style.backgroundColor = '#eee';
// Register onclick listener
tooltip.onclick = function() { alert('hello'); }
// *** Comment one of these out: ***
//document.body.appendChild(tooltip);
document.getElementById('container').appendChild(tooltip);
}
</script>
</head>
<body>
<div id="container" style="border: 1px solid blue; float: left; ">
<div id="myDiv"
onmouseover="makeDiv();"
style="position: relative; border: 1px solid red; width: 200px;">
<span>my div text</span>
</div>
</div>
</body>
</html>
Here is some code to remove the tooltip for onmouseout.
Give your toolTip an ID when creating it:
toolTip.setAttribute('id','toolTip');
Then for onmouseout
function removeDiv(container) {
var toolTip = document.getElementById('toolTip');
document.getElementById(container).removeChild(toolTip);
}

Categories

Resources