Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add click-and-drag to scale an image in an SVG?

I want to let visitors scale the image using click-and-drag:

<html>
    <body>
        <svg>
            <image>

Is there a JavaScript library that will help me?

I notice Raphael has click and drag methods but I will still have to write a lot of JS to:

  1. Find the corners of the image
  2. Make the area near the corners selectable
  3. Draw icons for the corners so it's obvious they are selectable
  4. Add click, drag and drop to the corners
  5. Update the dimensions of the image

I could do all that but it sounds time-consuming. Is there an open-source script I should use? Other suggestions?

like image 505
ram1 Avatar asked Sep 04 '11 18:09

ram1


People also ask

How do I drag an image into HTML?

The image link is inserted in the HTML page using <img> src attribute. Whenever we are going to insert the image, we have to enable draggable=”true”. Also, enable ondragstart=”drag(event)” so that this image can be draggable along with setting the image width and height.

How do you drag and drop in JavaScript?

Introduction to JavaScript Drag and Drop API To drag an image, you simply hold the mouse button down and then move it. To drag the text, you need to highlight some text and drag it in the same way as you would drag an image.

What is interact js?

interact. js is a JavaScript module for Drag and drop, resizing and multi-touch gestures with inertia and snapping for modern browsers (and also IE8+).


1 Answers

I found this solution at this post: Draggables and Resizables in SVG.

In the answer, you can find a jsfiddle link: http://jsfiddle.net/tmkfs/. You can resize the box by clicking and dragging the corner of the box. Hope this code could help you.

like image 88
cabreracanal Avatar answered Oct 12 '22 23:10

cabreracanal