React UI - 05 - Draggable SVG Path Generator
How do I make an react ui - 05 - draggable svg path generator?
What is a react ui - 05 - draggable svg path generator? How do you make a react ui - 05 - draggable svg path generator? This script and codes were developed by Xavier Martínez on 31 October 2022, Monday.
React UI - 05 - Draggable SVG Path Generator - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>React UI - 05 - Draggable SVG Path Generator</title> <link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="container"></div> <script src='https://npmcdn.com/[email protected]/dist/react.min.js'></script>
<script src='https://npmcdn.com/[email protected]/dist/react-dom.min.js'></script>
<script src='https://npmcdn.com/react-draggable'></script> <script src="js/index.js"></script>
</body>
</html>
React UI - 05 - Draggable SVG Path Generator - Script Codes CSS Codes
body { margin: 0;
}
#container { width: 100%; height: 100%; text-align: center;
}
svg { position: absolute; pointer-events: none; left: 0; top: 0; width: 100%; height: 100%; stroke-width: 3; stroke: black;
}
svg circle { pointer-events: auto !important; cursor: -webkit-grab;
}
svg circle:active { cursor: -webkit-grabbing;
}
.btn { position: relative; border: none; padding: 10px; cursor: pointer; width: 150px; margin: 10px; z-index: 1000;
}
.viewport { position: absolute; top: 0; left: 0; width: 100vw; height: 100vh; background-color: white; overflow: hidden; z-index: -2;
}
.card { cursor: -webkit-grab; position: absolute; background-color: lightgrey; width: 200px; height: 260px; z-index: -1;
}
.card:active { cursor: -webkit-grabbing;
}
React UI - 05 - Draggable SVG Path Generator - Script Codes JS Codes
'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var paths = [];
var imageurls = [];
var cards = [];
var adding = [];
var cardcount = 0;
var clickanchor = 0;
var SEARCH_KEY = 'cat';
var API_KEY = "afea92ac6eff442ba6fca79368717d0d";
var CARD_WIDTH = 200;
var CARD_HEIGHT = 260;
var MAX_CARDS = 20;
//Generate random image cards
(function generateCards() { for (var i = 0; i < MAX_CARDS; i++) { var url = 'https://api.giphy.com/v1/gifs/random?tag=' + SEARCH_KEY + '&api_key=' + API_KEY; fetch(url).then(function (response) { return response.json(); }).then(function (res) { return imageurls.push(res.data.image_url); }).catch(function (res) { console.log('ERROR: No image from API!'); }); }
})();
function pointIsCard(x, y) { var cardlist = cards.filter(function (card) { var pos_x = card.x + card.movedX; var pos_y = card.y + card.movedY; return x <= pos_x + CARD_WIDTH / 2 && x >= pos_x - CARD_WIDTH / 2 && y <= pos_y + CARD_HEIGHT / 2 && y >= pos_y - CARD_HEIGHT / 2; }); if (cardlist.length > 0) { return true; } else { //Don't draw }
}
var SVGPath = function (_React$Component) { _inherits(SVGPath, _React$Component); function SVGPath(props) { _classCallCheck(this, SVGPath); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.handleDragOrigin = _this.handleDragOrigin.bind(_this); _this.handleDragEnd = _this.handleDragEnd.bind(_this); _this.state = { origin: _this.props.origin, end: _this.props.end }; return _this; } SVGPath.prototype.handleDragOrigin = function handleDragOrigin(e, ui) { var _state$origin = this.state.origin; var x = _state$origin.x; var y = _state$origin.y; console.log({ x: x, y: y }); this.setState({ origin: { x: x + ui.deltaX, y: y + ui.deltaY } }); }; SVGPath.prototype.handleDragEnd = function handleDragEnd(e, ui) { var _state$end = this.state.end; var x = _state$end.x; var y = _state$end.y; console.log({ x: x, y: y }); this.setState({ end: { x: x + ui.deltaX, y: y + ui.deltaY } }); }; SVGPath.prototype.render = function render() { return React.createElement( 'div', null, React.createElement( 'svg', { xmlns: 'http://www.w3.org/2000/svg', height: '100vh', width: '100vw' }, React.createElement('path', { d: 'M ' + this.state.origin.x + ' ' + this.state.origin.y + ' L ' + this.state.end.x + ' ' + this.state.end.y }), React.createElement( ReactDraggable, { onDrag: this.handleDragOrigin }, React.createElement('circle', { cx: this.props.origin.x, cy: this.props.origin.y, r: '10', fill: 'red' }) ), React.createElement( ReactDraggable, { onDrag: this.handleDragEnd }, React.createElement('circle', { cx: this.props.end.x, cy: this.props.end.y, r: '10', fill: 'green' }) ) ) ); }; return SVGPath;
}(React.Component);
var Card = function (_React$Component2) { _inherits(Card, _React$Component2); function Card(props) { _classCallCheck(this, Card); var _this2 = _possibleConstructorReturn(this, _React$Component2.call(this, props)); _this2.state = { deltaPosition: { x: 0, y: 0 } }; _this2.handleMoveCard = _this2.handleMoveCard.bind(_this2); return _this2; } Card.prototype.handleMoveCard = function handleMoveCard(e, ui) { cards[this.props.index].movedX = ui.x; cards[this.props.index].movedY = ui.y; }; Card.prototype.render = function render() { return React.createElement( ReactDraggable, { onDrag: this.handleMoveCard }, React.createElement('div', { className: 'card', style: { backgroundImage: 'url(' + this.props.img + ')', backgroundSize: 'cover', backgroundPosition: '50% 50%', left: this.props.anchorX - CARD_WIDTH / 2, top: this.props.anchorY - CARD_HEIGHT / 2 } }) ); }; return Card;
}(React.Component);
var App = function (_React$Component3) { _inherits(App, _React$Component3); function App(props) { _classCallCheck(this, App); var _this3 = _possibleConstructorReturn(this, _React$Component3.call(this, props)); _this3.state = { drawing: false, anchoring: false, dropping: false }; _this3.checkOperation = _this3.checkOperation.bind(_this3); _this3.toggleDrawing = _this3.toggleDrawing.bind(_this3); _this3.toggleDropCard = _this3.toggleDropCard.bind(_this3); return _this3; } App.prototype.checkOperation = function checkOperation(e) { //Draw Anchor if (this.state.drawing && pointIsCard(e.clientX, e.clientY)) { var anchorstate = window.clickanchor; switch (anchorstate) { case 0: window.adding.origin = { x: e.clientX, y: e.clientY }; clickanchor++; break; case 1: window.adding.end = { x: e.clientX, y: e.clientY }; var addedanchor = { origin: window.adding.origin, end: window.adding.end }; window.paths.push(addedanchor); this.setState(function () { { anchoring: true; } }); clickanchor = 0; break; } } //Drop Card if (this.state.dropping && cardcount < MAX_CARDS) { window.cards.push({ x: e.clientX, y: e.clientY, movedX: 0, movedY: 0, img: imageurls[cardcount] }); this.setState(function () { { dropping: false; } }); cardcount++; } }; App.prototype.toggleDrawing = function toggleDrawing(e) { var _this4 = this; e.stopPropagation(); this.setState(function () { if (_this4.state.drawing) { return { drawing: false, dropping: false }; } else { return { drawing: true, dropping: false }; } }); }; App.prototype.toggleDropCard = function toggleDropCard(e) { var _this5 = this; e.stopPropagation(); this.setState(function () { if (_this5.state.dropping) { return { drawing: false, dropping: false }; } else { return { drawing: false, dropping: true }; } }); }; App.prototype.render = function render() { return React.createElement( 'div', { className: 'viewport', onClick: this.checkOperation }, React.createElement( 'button', { style: this.state.drawing ? { backgroundColor: 'red', color: 'white' } : { backgroundColor: 'lightgrey', color: 'black' }, onClick: this.toggleDrawing, className: 'btn' }, 'Draw Anchor' ), React.createElement( 'button', { style: this.state.dropping ? { backgroundColor: 'red', color: 'white' } : { backgroundColor: 'lightgrey', color: 'black' }, onClick: this.toggleDropCard, className: 'btn' }, 'Drop Card' ), window.paths.map(function (anchor, index) { return React.createElement(SVGPath, { key: index, origin: anchor.origin, end: anchor.end }); }), window.cards.map(function (card, index) { return React.createElement(Card, { key: index, index: index, anchorX: card.x, anchorY: card.y, img: card.img }); }) ); }; return App;
}(React.Component);
ReactDOM.render(React.createElement(App, null), document.getElementById('container'));
![React UI - 05 - Draggable SVG Path Generator - Script Codes](http://shots.codepen.io/xmjol/pen/awGrxo-512.jpg)
Developer | Xavier Martínez |
Username | xmjol |
Uploaded | October 31, 2022 |
Rating | 3 |
Size | 6,723 Kb |
Views | 26,312 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
A beautiful day | 2,973 Kb |
ReactJS Simple Component | 2,540 Kb |
Flex2x2 | 1,883 Kb |
Pixel Anim Test | 2,415 Kb |
Background Styles | 3,501 Kb |
React UI - 02 - Modal trigger | 4,053 Kb |
SVG Business Card | 3,661 Kb |
4-block-flex Round | 3,301 Kb |
Bottom Menu- Social | 2,245 Kb |
React UI - 03 - CardList | 8,219 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Formations | Cantelope | 5,731 Kb |
Responsive Table-less Shopping Cart | Alex_rodrigues | 6,637 Kb |
Angular Route | Arun_v606 | 1,837 Kb |
Weird glowy CSS3 game | Toneworm | 3,684 Kb |
AngularJS Datalist Directive | M-e-conroy | 2,366 Kb |
Slide like Mailbox | Hmps | 3,758 Kb |
About Mazano | Kiti | 2,585 Kb |
Vue.js Lazy Loading | Kjbrum | 3,620 Kb |
No Vacancy 404 CSS Only | Sethkontny | 0 Kb |
Learning canvas drawing | Aurer | 2,204 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!