Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                PIXI.DisplayObject.prototype.addEventListener = function(events, listener) {
  events.split(' ').forEach(event => this.on(event, listener));
};
Number.prototype.clamp = function(min, max) {
  if (this < min) return min;
  if (this > max) return max;
  return this;
};
window.getDevicePixelRatio = function() {
  var ratio = 1;
  // To account for zoom, change to use deviceXDPI instead of systemXDPI
  if (window.screen.systemXDPI !== undefined && window.screen.logicalXDPI !== undefined && window.screen.systemXDPI > window.screen.logicalXDPI) {
    // Only allow for values > 1
    ratio = window.screen.systemXDPI / window.screen.logicalXDPI;
  } else if (window.devicePixelRatio !== undefined) {
    ratio = window.devicePixelRatio;
  }
  return ratio;
};

let DEBUG_GRID_INDEX = 0;

setTimeout(init, 0); //set up scene when target element is rendered and CSS has been applied

let rendererOptions = {
  resolution: getDevicePixelRatio(),
  backgroundColor: 0xF78E69
};

function init() {
  let renderer = new PIXI.autoDetectRenderer(1920, 1200, rendererOptions);
  renderer.view.addEventListener("contextmenu", e => e.preventDefault());
  document.body.appendChild(renderer.view);
  createjs.Ticker.framerate = 60; 
  
  let stage = new PIXI.Container();
  let desktopLayer = new PIXI.Container();
  let desktop1 = new PIXI.Graphics().beginFill(0x000000, 1).drawRect(renderer.width, 0, renderer.width, renderer.height).endFill();
  let desktop1Bg = new PIXI.Sprite.from("https://i.imgur.com/g2VXR4e.jpg");
  let desktop2 = new PIXI.Graphics().beginFill(0xA7E488, 1).drawRect(renderer.width, 0, renderer.width, renderer.height).endFill();
  let itemContainer = new PIXI.Container();
  let wonderfulRectangle = new PIXI.Graphics().beginFill(0xABE188, 1).drawRoundedRect(0, 0, renderer.width / 6, renderer.height / 5, 10).endFill();
  let handles = new PIXI.Container();
  let handleRect = new PIXI.Graphics().lineStyle(5, 0xFF0000).drawRect(wonderfulRectangle.x, wonderfulRectangle.y, wonderfulRectangle.width, wonderfulRectangle.height);
  let handle1 = new PIXI.Graphics().beginFill(0xFF0000, 1).drawCircle(handleRect.x, handleRect.y, 10).endFill();  
  let handle2 = new PIXI.Graphics().beginFill(0xFF0000, 1).drawCircle(handleRect.x, handleRect.y+wonderfulRectangle.height, 10).endFill();  
  let handle3 = new PIXI.Graphics().beginFill(0xFF0000, 1).drawCircle(handleRect.x+wonderfulRectangle.width, handleRect.y, 10).endFill();  
  let handle4 = new PIXI.Graphics().beginFill(0xFF0000, 1).drawCircle(handleRect.x+wonderfulRectangle.width, handleRect.y+wonderfulRectangle.height, 10).endFill();
  let texture = new PIXI.Texture.fromImage("https://i.imgur.com/jWr67J8.png?1");
  let lel = new PIXI.Sprite(texture);
  
  handleRect.addChild(handle1);  
  handleRect.addChild(handle2);
  handleRect.addChild(handle3);
  handleRect.addChild(handle4);
  
  handles.addChild(handleRect);
  
  wonderfulRectangle.addChild(lel);
  
  itemContainer.addChild(wonderfulRectangle);  
  itemContainer.addChild(handles);
  
  desktop1.addChild(desktop1Bg);  
  desktop1.addChild(itemContainer);  
  
  desktopLayer.addChild(desktop1);
  desktopLayer.addChild(desktop2); 
  
  stage.addChild(desktopLayer);
  
  desktop1.calculateBounds = function() {
    if (this.fixedBounds) {
      let bounds = [this._bounds.minX, this._bounds.minY, this._bounds.maxX, this._bounds.maxY];
      let valid = bounds.forEach(n => (n != Infinity && typeof n != 'undefined' && n != null));
      if (valid) {
        console.log(this._bounds);
        this._bounds.minX = bounds[0];
        this._bounds.minY = bounds[1];
        this._bounds.maxX = bounds[2];
        this._bounds.maxY = bounds[3];
      } else {
        PIXI.Graphics.prototype.calculateBounds.call(this);
      }
    } else PIXI.Graphics.prototype.calculateBounds.call(this);
  };
  desktop1Bg.height = desktop1.height;
  
  desktopLayer.interactive = true;
  desktopLayer.tag = "dsl";  
  
  wonderfulRectangle.interactive = true;
  wonderfulRectangle.buttonMode = true;
  wonderfulRectangle.defaultCursor = "move";
  
  handle4.interactive = true;
  handle4.buttonMode = true; 
  handle4.defaultCursor = "nwse-resize";
  
  lel.width = wonderfulRectangle.width * 0.8;
  lel.height = wonderfulRectangle.height * 0.85;
  lel.setTransform(lel.parent.x + 30, lel.parent.y + 10);
    
  desktopLayer.addEventListener('rightclick', function(event) {
    if (event.target.tag == "dsl") {
      if (event.target.position.x == 0)
        createjs.Tween.get(event.target.position, {
          override: true
        }).to({
          x: -event.target.width / 2
        }, 500, createjs.Ease.getPowIn(2.2));
      else createjs.Tween.get(event.target.position, {
        override: true
      }).to({
        x: 0
      }, 500, createjs.Ease.getPowIn(2.2));
    }
  });
 
  wonderfulRectangle.addEventListener('mouseover', event => {
    clearTimeout(event.target.resetTimeout)
    createjs.Tween.removeAllTweens(event.target);
  });
  wonderfulRectangle.addEventListener('mousedown touchdown', function(event) {
    event.target.moving = true;
    desktop1.fixedBounds = true;
    event.target.lastPointerPos = event.data.getLocalPosition(event.target.parent);
  });
  wonderfulRectangle.addEventListener('mouseup touchend', function(event) {
    event.target.moving = false;
    let snapPoint = getNearestSnapPoint(6, 5, event.target.parent, renderer);
    createjs.Tween.get(event.target.parent.position, {
      override: true
    }).to(snapPoint, 500, createjs.Ease.getPowIn(2.2));
    desktop1.fixedBounds = false;
    event.target.lastPointerPos = null;
  });
  wonderfulRectangle.addEventListener('mouseout touchendoutside', (event) => {
    if (event.target && event.target.moving) {
      // Give some leeway if the pointer moves faster than RAF can update the canvas and move the object back under pointer      
      event.target.resetTimeout = setTimeout(() => {        
        event.target.moving = false;
        let snapPoint = getNearestSnapPoint(6, 5, event.target.parent, renderer);
        createjs.Tween.get(event.target.parent.position, {
          override: true
        }).to(snapPoint, 500, createjs.Ease.getPowIn(2.2));
        desktop1.fixedBounds = false;
        event.target.lastPointerPos = null;
      }, 80);
    }
  });
  wonderfulRectangle.addEventListener('mousemove touchmove', function(event) {
    if (event.target && event.target.moving) {
      let newPoint = event.data.getLocalPosition(desktop1); //get cursor position relative to desktop (why is this function name so ploughing misleading?)
      let dx = newPoint.x - event.target.lastPointerPos.x;
      let dy = newPoint.y - event.target.lastPointerPos.y;
      event.target.parent.position.x += dx;
      event.target.parent.position.y += dy;
      event.target.lastPointerPos = newPoint;
    }
  });
  
  handle4.addEventListener("mousedown mouseover", event =>{
    event.stopPropagation();
  });
  
  requestAnimationFrame(() => {
    update(renderer, stage)
  });
}

function update(renderer, stage) {  
  renderer.render(stage);
  requestAnimationFrame(() => {
    update(renderer, stage)
  });
}
function getNearestSnapPoint(cellsX, cellsY, target, renderer) {
  let cellWidth = Math.floor(renderer.width / cellsX);
  let cellHeight = Math.floor(renderer.height / cellsY);
  //let global = target.toGlobal(new PIXI.Point(0,0));
  let snapPosX = cellWidth * (((target.x % cellWidth) > cellWidth / 2) ? Math.ceil(target.x / cellWidth) : Math.floor(target.x / cellWidth));
  let snapPosY = cellHeight * (((target.y % cellHeight) > cellHeight / 2) ? Math.ceil(target.y / cellHeight) : Math.floor(target.y / cellHeight));
  //if (renderer) drawDebugLines(renderer, target.parent, cellsX, cellsY);
  return new PIXI.Point(snapPosX.clamp(0, renderer.width - cellWidth), snapPosY.clamp(0, renderer.height - cellHeight));
}
function drawDebugLines(renderer, container, cellsX, cellsY) {
  let cellWidth = Math.floor(renderer.width / cellsX);
  let cellHeight = Math.floor(renderer.height / cellsY);
  let lines;
  try {
    lines = container.getChildAt(DEBUG_GRID_INDEX);
    if (lines.tag != "DEBUG_GRID") lines = new PIXI.Graphics();
  } catch (e) {
    lines = new PIXI.Graphics();
  }
  lines.tag = "DEBUG_GRID";
  for (let i = 0; i < cellsY; i++) {
    lines.moveTo(0, cellHeight * i).lineStyle(2, 0x5D675B).lineTo(renderer.width, cellHeight * i);
  }
  for (let j = 0; j < cellsX; j++) {
    lines.moveTo(cellWidth * j, 0).lineStyle(2, 0x5D675B).lineTo(cellWidth * j, renderer.height);
  }
  container.boundsPadding = 0;
  container.addChild(lines);
  if (container.getChildIndex(lines) != DEBUG_GRID_INDEX) {
    container.swapChildren(container.getChildAt(DEBUG_GRID_INDEX), lines);
  }
}
              
            
!
999px

Console