Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

d3-drag

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

d3-drag

Drag and drop SVG, HTML or Canvas using mouse or touch input.

  • 0.2.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.1M
increased by3.43%
Maintainers
1
Weekly downloads
 
Created

What is d3-drag?

The d3-drag module in D3.js provides a way to handle drag-and-drop interactions for SVG elements. It allows you to create drag behaviors that can be applied to elements, enabling you to move them around within an SVG canvas. This is particularly useful for creating interactive visualizations where elements need to be repositioned by the user.

What are d3-drag's main functionalities?

Basic Dragging

This code snippet demonstrates how to enable basic dragging for a circle element. When the circle is dragged, its 'cx' and 'cy' attributes are updated to the current mouse position.

d3.select('circle').call(d3.drag().on('drag', function(event) { d3.select(this).attr('cx', event.x).attr('cy', event.y); }));

Drag Start and End Events

This example shows how to handle drag start and end events. When the drag starts, a message is logged to the console, and another message is logged when the drag ends.

d3.select('circle').call(d3.drag().on('start', function(event) { console.log('Drag started'); }).on('end', function(event) { console.log('Drag ended'); }));

Constrained Dragging

This code snippet demonstrates how to constrain the dragging of a circle within the bounds of an SVG canvas. The 'cx' and 'cy' attributes are clamped to ensure the circle stays within the specified width and height.

d3.select('circle').call(d3.drag().on('drag', function(event) { var x = Math.max(0, Math.min(width, event.x)); var y = Math.max(0, Math.min(height, event.y)); d3.select(this).attr('cx', x).attr('cy', y); }));

Other packages similar to d3-drag

Keywords

FAQs

Package last updated on 01 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc