var svg = document.querySelector('.container'); var svgns = 'http://www.w3.org/2000/svg'; var path = document.createElementNS(svgns, 'path'); svg.appendChild(path); var points = []; var Resizer_Instance = null; var boundingBox = svg.getBoundingClientRect(); var toSVGPath = function(points) { var SVGPath = ''; for (var i = 0; i < points.length; i++) { var prefix = (i == 0) ? 'M' : 'L'; SVGPath += prefix + points[i].x + ' ' + points[i].y + ' '; } return SVGPath; }; var create_mousedown = false; var createStart = function(event) { create_mousedown = true; }; var creating = function(event) { if (create_mousedown) { var point = svg.createSVGPoint(); point.x = event.clientX - boundingBox.left; point.y = event.clientY - boundingBox.top; var t = point.matrixTransform(svg.getScreenCTM().inverse()); points.push(t); path.setAttributeNS(null, 'd', toSVGPath(points)); } }; var createEnd = function(event) { create_mousedown = true; svg.removeEventListener('mousedown', createStart); svg.removeEventListener('mousemove', creating); svg.removeEventListener('mouseup', createEnd); setTimeout(function functionName() { Resizer_Instance = new Resizer(path, svg); }, 500); }; svg.addEventListener('mousedown', createStart); svg.addEventListener('mousemove', creating); svg.addEventListener('mouseup', createEnd); var Resizer = (function() { function Resizer(element) { var that = this; that.element = element; createSelector.call(that); document.addEventListener('mousemove', dragging); document.addEventListener('mouseup', dragend); } var RAD2DEG = 180 / Math.PI; function angleBetweenPoints(p1, p2) { var angle = null; if (p1.x == p2.x && p1.y == p2.y) angle = Math.PI / 2; else angle = Math.atan2(p2.y - p1.y, p2.x - p1.x); return (angle * RAD2DEG) + -90; } function controlPositions(el) { var pt = svg.createSVGPoint(); var bbox = el.getBoundingClientRect(); var matrix = el.getScreenCTM().inverse(); var halfWidth = bbox.width / 2; var halfHeight = bbox.height / 2; var placements = {}; pt.x = bbox.left; pt.y = bbox.top; placements['nw'] = pt.matrixTransform(matrix); pt.x += halfWidth; placements['n'] = pt.matrixTransform(matrix); pt.x += halfWidth; placements['ne'] = pt.matrixTransform(matrix); pt.y += halfHeight; placements['e'] = pt.matrixTransform(matrix); pt.y += halfHeight; placements['se'] = pt.matrixTransform(svg.getScreenCTM().inverse()); pt.x -= halfWidth; placements['s'] = pt.matrixTransform(matrix); pt.x -= halfWidth; placements['sw'] = pt.matrixTransform(matrix); pt.y -= halfHeight; placements['w'] = pt.matrixTransform(matrix); pt.x += halfWidth; placements['center'] = pt.matrixTransform(matrix); pt.y -= (halfHeight + 30); placements['rot'] = pt.matrixTransform(matrix); return placements; } var dragging_element = null; var dragstart = function(event) { var box = this; var context = box.context; var rootContext = context.rootContext; rootContext.current_handle_inaction = context.direction; dragging_element = box; }; var dragging = function(event) { if (!dragging_element) return; var box = dragging_element; var context = box.context; var rootContext = context.rootContext; var currentHandle = rootContext.current_handle_inaction; var control_points = rootContext.control_points; if (currentHandle === context.direction) { var point = svg.createSVGPoint(); point.x = event.clientX; point.y = event.clientY; var element = rootContext.element; var transformed = point.matrixTransform(svg.getScreenCTM().inverse()); var centerPosition = context.center; rootContext.angle = rootContext.angle || 0; rootContext.hMove = rootContext.hMove || 0; rootContext.vMove = rootContext.vMove || 0; rootContext.scaleX = rootContext.scaleX || 1; rootContext.scaleY = rootContext.scaleY || 1; switch (currentHandle) { case "rot": rootContext.angle = angleBetweenPoints(transformed, centerPosition); break; case "center": rootContext.hMove = transformed.x - centerPosition.x; rootContext.vMove = transformed.y - centerPosition.y; break; case "s": var startPos = control_points[currentHandle]; var vMove = transformed.y - startPos.y; rootContext.scaleY += (vMove > 0 ? -1 : 1) * 0.001; break; } var move_transform = "translate(" + rootContext.hMove + " " + rootContext.vMove + ")"; var rotate_transform = "rotate(" + rootContext.angle + ", " + centerPosition.x + ", " + centerPosition.y + ")"; var scale_transform = "scale(" + rootContext.scaleX + ", " + rootContext.scaleY + ")"; var transform = [move_transform, rotate_transform, scale_transform].join(' '); rootContext.element.setAttribute('transform', transform); rootContext.controlGroup.setAttribute('transform', transform); } }; var dragend = function() { if (!dragging_element) return; var box = dragging_element; var context = box.context; var rootContext = context.rootContext; delete rootContext.current_handle_inaction;
path { fill: none; stroke: #42B6DF; } body, html { height: 100%; width: 100%; margin: 0; }
<svg class="container" version="1.1" baseProfile="full" style="position:absolute;left:0;top:0;height:100%;width:100%;-ms-transform:scale(1,1);transform:scale(1,1);-webkit-transform:scale(1,1);-moz-transform:scale(1,1);-o-transform:scale(1,1);transform:scale(1,1);-ms-transform-origin:0, 0;-webkit-transform-origin:0, 0;-moz-transform-origin:0, 0;-o-transform-origin:0, 0;transform-origin:0, 0" viewBox="-220.38356461849224 6442.3347962008365 454.7376658611161 114.54981723871151"></svg>