cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

              <p>Arrastre el rectángulo amarillo.</p>
<svg width="240" height="240" viewBox="0 0 240 240">
              body {
  background-color: #333;
  color: #d9d9d9;
  font-family: Arial, sans-serif;
  text-align: center;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  line-height: 200%;

svg {
  border: 1px solid #777;
  display: block;
  margin: 20px auto;
svg *{cursor:move;}
              var SVG_NS = 'http://www.w3.org/2000/svg';
var svg = document.querySelector("svg");
var svgRaton = {x:0, y:0};// la posición del ratón relativo al lienzo SVG
var rectRaton = {x:0, y:0};// la posición del ratón relativo al rectángulo
var distInicial = {x:0, y:0};// la posición inicial del rectángulo
var arrastrar = false;

function oMousePos(elemento, evt) {
  var ClientRect = elemento.getBoundingClientRect();
  return { //objeto
    x: Math.round(evt.clientX - ClientRect.left),
    y: Math.round(evt.clientY - ClientRect.top)

function Rect() {
  this.att = {};

  this.dibujar = function(elementoPadre) {
    var elmt = document.createElementNS(SVG_NS, "rect");
    for (var name in this.att) {
      if (this.att.hasOwnProperty(name)) {
        elmt.setAttributeNS(null, name, this.att[name]);
    this.elemento = elmt;
  this.actualizar = function(x,y){
    this.elemento.setAttributeNS(null, "transform", "translate("+x+","+y+")");
}//function Rect()

  var oRect = new Rect();
  // los atributos del rectángulo
  oRect.att = {x:80,y:94,width:80,height:50, transform:"translate(0,0)",fill:"gold"};
  // dibuja el rectángulo

  var rectangulo = svg.querySelector("rect");

     var num = 0;
     rectangulo.addEventListener("mousedown", function(evt){
       svgRaton = oMousePos(svg, evt); 
       rectRaton = oMousePos(this, evt);
       if(num == 1){       // calculado solo una vez, en el primer clic
       // recupera la posición inicial del trazado
       distInicial.x = svgRaton.x - rectRaton.x;
       distInicial.y = svgRaton.y - rectRaton.y;
       arrastrar = true;
     }, false);
     rectangulo.addEventListener("mousemove", function(evt) {
       if (arrastrar) {
        svgRaton = oMousePos(svg, evt);
         var x = svgRaton.x - rectRaton.x - distInicial.x;
         var y = svgRaton.y - rectRaton.y - distInicial.y;
        oRect.actualizar( x, y);  
       }, false);
       rectangulo.addEventListener("mouseup", function(evt) {
       arrastrar = false;
       }, false);

       rectangulo.addEventListener("mouseout", function(evt) {
       arrastrar = false;
       }, false); 

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.


Loading ..................