css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <p>Arrastre el rectángulo amarillo.</p>
<svg width="240" height="240" viewBox="0 0 240 240">
</svg>
            
          
!
            
              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.elemento;

  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]);
      }
    }
    elementoPadre.appendChild(elmt);
    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
  oRect.dibujar(svg);

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

     var num = 0;
     rectangulo.addEventListener("mousedown", function(evt){
       num++
       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); 
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console