resize and draggable the group of the SVG element - svg

I am working on a SVG application and now want to let a user resize an object using draggable corners, very much like in SVG-edit (http://svg-edit.googlecode.com/svn/trunk/editor/svg-editor.html).
The functionality should work as follows: The user selects an SVG object, drags it to the main ‘canvas’ and once on the ‘canvas’, four corners on the outside of the object appear, the user can drag on each of the corner points & drag to enlarge the object. The objects will be rectangular in shape and created using paths, not using the SVG ‘rect’ function.
Would anyone have any suggestions as to how this should be implemented?
Thanks in Advance

You could take a look at some existing open source implementations. svg-edit is one which you already mentioned.
To showcase a tool that I developed, I wrote a demo which implements this functionality. This which may serve as a simpler and more restrictive example than svg-edit, as it doesn't do too much other than allow you to draw rects and circles, and rotate/translate/scale them.

I have edited jquery to make rect draggable, recently put them in a g and lost this functionality. The g doesn't contain the position data.
Might be possible to bubble down to the children elements.

Related

Can you hide a node when not directly above a parent in Godot?

So I have an instanced scene that is supposed to be the child of a colour rect in my tree. I want to randomly generate the nodes, but I also want parts of the view to be cut off if the texture no longer is above the main section. I know you can render nodes below their parents, but I don't know if stopping part of them from rendering is physically possible.
In this image I want the bottom circle to remain the same, but the top circle to not show anything above the dark purple box
This is the node tree in the editor
Is there any way to do this directly, or am I gonna have to use a viewport of some variety?
I believe what you want is to set rect_clip_content to true on the ColorRect (or whatever Control). Making invisible any part of its children outside of it.
From Godot's documentation:
bool rect_clip_content
Enables whether rendering of CanvasItem based children should be clipped to this control's rectangle. If true, parts of a child which would be visibly outside of this control's rectangle will not be rendered.
If what you want is the opposite, perhaps you can use z_index to have something render on top, occluding the parts you don't want visible.
There is also a trick you can use with lights (including 2D lights):
Make a light that matches the area you want things to be visible.
Set a custom material that will be transparent by default, but visible on the light pass. The simpler way to do this is to set the light_mode of the material to "Light Only". You could also do it with a custom shader instead.
Making something disappear with light, in 2D, is impossible. In 3D, you can use flags_use_shadow_to_opacity. That is how you make a shadow catcher.
But, there is one more trick: you can use a mask. This should give you full control of when to show or hide things. So, if none of the above solutions works for you, use a mask. I have an explanation in a different answer. See also: How to crop sprite in a non-rectangular form?.
Mighty Mochi Games recently (2022-03-30) made a compilation of the different approaches in video form: Mask Methods Collection - Godot 3.x - 2D
.

svg elements - scroll to/ centering

I've an svg dynamically created on a page. As things "happen" (user clicks) the svg expands and collapses certain elements. It may fit in the viewport, it may not. In the case that its too big to fit on a page, the user must scroll to where s(he) wants to go/see. Now this is fine, however I have a requirement that the last element "selected" becomes the center of the page/viewport. i.e. If they click on an item, thats what they need to see without scrolling.
Could anybody tell me the best way to attack this. I've googled around but can't find what I'm looking for (though I'm not long at all this so I might have been searching the wrong stuff).
Is there a way to do this purely programmatically with javascript? Or am I obliged to pass by CSS to get the solution I want. Any tips/links/advice much appreciated.
thanks and have a nice day
G
I had a similar thing and I used the viewBox property to handle this. You could also use a wrapping <g> element, which you translate. However, from my point of view the basic approach is the same and you basically need to do two things:
keep track of the x and y offset and the dimensions of the viewport. (Using the viewBox will give you that »for free«)
Compute the center of the Element. Therefore I used the getBoundingClientRect()method which yields the AABB of of the Element in absolute coordinate space, relative to top-left edge of the whole page.
With these things, all that remains is to calculate the vector from the viewport center to the object's center.
Here you can find the reference of the viewBox and here a nice tutorial about it, because it can be a bit confusing at the beginning.
Another pro for the »viewBox« approach is: There is no dependency on special DOM elements, it just works on the root <svg> element. I once implemented both methods, I started out using a wrapping <g> element, what worked fine but gave me some performance issues. So I decided to change and use the viewBox, with the result, that the performance in Firefox grow, but slowed down in Chromium.
Edit
Here you can find a little fiddle, that outlines the approach. But be aware of the following: getBoundingClientRect() yields the position of the Element on the whole Page, so if your <svg> is not positioned at (0,0) (top: 0px; left: 0px), than that will include the offset of the svg itself. The offset of the viewBox must not include this offset, so you need to cancel that out somehow. For sake of simplicity I just used the client Bounding Rect of the SVG, what works because there are no transformations applied.

How to Scale SVG rectagle to fit the svg text element

So here is the problem:
I am trying to create dynamic buttons that have text. The text will be generated dynamically so the svg object doesn't know the size of the text. There are two things that I am looking to do and I hope that SVG will do this
First I want the left and right edge of the svg element to stay the same even if I scale the element horizontally
The problem is that I have to set a width on the svg otherwise it doesn't show up when I display the page. Also on the Home and blog buttons you can see that the edge is compressed. I want the edge to stay the same no matter how much text is in the element.
Also I can't seem to set the scale or width properly even with a javascript .getComputedTextLength()
Any help or a point in the right direction would be very helpful
Buttons that are sized to their text content is functionality that can be adressed with Raphael's getBBox()
The use of this js library means that you are implicitly using SVG or VML and this functionality is more easily addressed by referencing this JavaScript library
To see the getBBox() function in action you could visit the Autobox example here:
http://www.irunmywebsite.com/raphael/additionalhelp.php?v=2

foreignobject in svg

How to change the height and width of textarea (or any html element) from javascript which is inside foreign object in svg.Moreover, can I drag an html elelment and drop on svg container by any mean (htm5 any hint) or atleast can I get the coordinates on svg during draging so that I can implement my own drag and drop. Thanks in advance
You appear to my eye to have at least two distinct questions. If I understand correctly, the first one is something like this: how can you write JavaScript within an SVG instance so that you can embed the SVG instance and have the JS control the embedding HTML? Do I have that right?
If so, the answer can be constructed from the parts of http://phaseit.net/examples/SVG4/c1.html and related examples. Briefly, write "window.top" to reach the HTML reference.
The same sort of reference gives access to HTML5 drag-and-drop, if that's what you're after.
Is that the level of answer you're after, or do you need an executable example?
LATER, AFTER MORE DISCUSSION: it appears that http://phaseit.net/examples/SVG5/embedding.svg is more like what you're after. Do I have that right, Arslan Ahson? What you should see when you display the SVG instance there is an SVG-coded elliptical button which, when pushed, toggles the appearance (background color and width) of a nearby embedded HTML textarea.

Are there other options of enabling an irregular shaped clickable area on a web page other than image maps?

(This is a post that I'm moving from ui stackexchange.)
We have some irregular shaped areas...imagine a map of the states of the US. Imagine also that inside each state there is a rectangular region that contains graphics and text. We want to make each of the states react to mouseover/mouseout/click.
If it is a straight up HTML/Javascript page (no Flash), are there other ways to implement this than image maps?
If this is not possible, I would also consider just having the rectangular region within each state react to mouse events.
Does "straight up HTML" mean you can use Javascript? If so, consider the Raphael javascript library, which wraps SVG. Check out their homepage for a great overview of what you can do, including this example of counties (?) of Australia: http://raphaeljs.com/australia.html
You could also consider processing.js, which is a similar visualisation library but wraps canvas as opposed to SVG.
how about coordinates? grab your map of the us, figure out the polygons that contain a state, and every time the user clicks somewhere, get the position of the click and figure out what polygon that position is in.
the only tricky part is doing the translation from logical to physical. i.e. when you get the location of a click it'll probably give you the location in global browser positioning, and you'll need to figure out where your image is using jquery's .position().

Resources