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>Drag the logo on the Test zone.<br />
  the code checks if the logo fits in the circle (or any image or shape)<br />
  It is pixel perfect and real time<br />
  Border will change from green  to red if shape leaves the circle.<br />
  zone is here to help you see the checked zone.</p>
  
<div id="draw-zone">
  <p class="zone-title">Test zone (drag logo to test)</p>
  <img id="draw-image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAA8CAMAAAAT6xnzAAAAn1BMVEUAAAAdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhwdHhxCk3BoAAAANHRSTlMAcOoOTDVvQ8o+8okYCPnvr2kd9dbTt3xZOTEUEN6Tg1QrJdu/m3hgR8+ic1Ig4cWon5eNrdYuyQAAAdlJREFUSMfdldl6gjAQhceAihsigijivuBetXn/Z2vDmCIjkHjRi/a/kRAOHzlnZoR85rzP+wt4g47Hvzm8IznyhLq+ImIo+dCXtPiDoa5iySW+ruTAfzjqKUZcUEu+rtvWkgzEsxNwE2VLRzGUB5+KX3sJavbS3nFi9VWt6GGI6WVDL8UbLnxxPVBJZskBzrgwuWBernD74qGZXDpi5ZVLNonBHbkMMSJ1ijFI0GhmqFK0IGXMFHnuXrtkywXFeVq0F6XRt/IUScej0dWChr/kfvegJM9A7F06QKjSPGmKPbkkRlu5KRbttLsF7wqJwdRoFgEBy8mBXDwsPIKZMZhwEntNl6ZYWhhrsbvJaXgWlZfrKJPiStyqAIEY7bykaClbz6TO76CELXnpp8ZY8DJ5LrggVE/EtMWdvKTysw7wuvGY1wYSZWrZkED9aVytE8mqy5A+Wz7/zcrbbGKL5+7ibswp00zshBBTJDTb6fx4wUGDKfdM5RFi6M1qlSy1IJZnP24rdLN1gn9MdbX3vRAak73vL2DILG8dKSQmzpk5dmENUyoFK6mByipKxn9VYlKJ+vjGlUpaoMTdcFNKAh4YoMO5LSVLF3SREuTXJCc6ttXsuG3bBcPwCx/ogF/Q9yJIAAAAAElFTkSuQmCC" />
  <canvas id="draw-background" width="400" height="300"></canvas>
</div>
<div id="result-zone">
 <p class="zone-title">Result zone</p>
 <canvas id="result" width="400" height="300"></canvas>  
</div>
<div id="zoom-zone">
 <p class="zone-title">Zoom</p>
 <canvas id="zoom" width="220" height="260"></canvas>
</div>
            
          
!
            
              /* Juste a little skin of display */
#draw-zone,
#result-zone,
#zoom-zone {
  position: relative;
  display: inline-block;
  vertical-align: top;
  width: 400px;
  height: 300px;
  margin: 10px;
  border: 3px solid black;
  background: #eee;
}

#draw-zone.ok {
  border-color: #0F0;
}
#draw-zone.nok {
  border-color: #F00;
}

#zoom-zone {
  width: 220px;
  height: 260px;
}

#draw-image {
  position: absolute;
  top: calc(50% - 25px);
  left: calc(50% - 30px);
  width: 50px;
  height: 60px;
  cursor: move;
}

.zone-title {
  position: absolute;
  top: 10px;
  left: 10px;
  margin: 0;
  pointer-events: none;
}

            
          
!
            
              // Inits elements
var drawCanvas = document.getElementById('draw-background');
var drawContext = drawCanvas.getContext('2d');
var drawZone = document.getElementById('draw-zone');
var shape = document.getElementById('draw-image');
var resultCanvas = document.getElementById('result');
var resultContext = resultCanvas.getContext('2d')
var zoomCanvas = document.getElementById('zoom');
var zoomContext = zoomCanvas.getContext('2d');
zoomContext.imageSmoothingEnabled = false;

// Draws test zone background + circle
var gradient = drawContext.createRadialGradient(100, 50, 10, 238, 50, 300);
gradient.addColorStop(0, '#aaaaff');
gradient.addColorStop(1, '#66c');
drawContext.fillStyle = gradient;
drawContext.fillRect(0, 0, drawCanvas.width, drawCanvas.height);
drawCircle(drawContext);

// Function to draw the circle in test or result zone
function drawCircle(context) {
  context.beginPath();
  context.arc(200, 150, 100, 0, 2 * Math.PI, false);
  var gradient = context.createRadialGradient(200,150,1,200,150,100);
  gradient.addColorStop(0,"#fcff00");
  gradient.addColorStop(1,"white");
  context.fillStyle = gradient;
  context.fill();
}

// Makes shape draggable. Checks its position when moving
jQuery(shape).draggable({
  containment: 'parent',
  create:() => {
    checkImagePosition();
  },
  start: () => {
    checkImagePosition();
  },
  drag:() => {
    checkImagePosition();
  }
});

function checkImagePosition() {
  // Fills background with a unique color not present on our test zone
  resultContext.fillStyle = "#aaaaff";
  resultContext.fillRect(0, 0, resultCanvas.width, resultCanvas.height);
  // Redraws the background n result zone
  drawCircle(resultContext);
  
  // Gets color data of the zone where the image will be drawn
  var colorData = resultContext.getImageData(shape.offsetLeft, shape.offsetTop, shape.width, shape.height).data;
  
  // Counts the number of pixels filled with our unique color 
  var backgroundPixelNumber = 0;
  for (var i = 0; i < colorData.length; i += 4) {
    if(colorData[i] === 170 && colorData[i + 1] === 170 && colorData[i + 2] === 255) {
      ++backgroundPixelNumber;
    }
  } 
  
  // Adds the image at its position (before you can return if backgroundPixelNumber === 0, no need to check if shape is fully on the circle)
  resultContext.drawImage(shape, shape.offsetLeft, shape.offsetTop, shape.width, shape.height);
  
  // Draws a zoom
  zoomContext.clearRect(0, 0, zoomCanvas.width, zoomCanvas.height);
  zoomContext.drawImage(resultCanvas, shape.offsetLeft - 5, shape.offsetTop - 5, shape.width + 10, shape.height + 10, 0, 0, zoomCanvas.width, zoomCanvas.height)
  // Gets the color data of the zone again, with image over
  colorData = resultContext.getImageData(shape.offsetLeft, shape.offsetTop, shape.width, shape.height).data;
  
  for (var i = 0; i < colorData.length; i += 4) {
    if(colorData[i] === 170 && colorData[i + 1] === 170 && colorData[i + 2] === 255) {
      --backgroundPixelNumber;
    }
  }
console.log(backgroundPixelNumber);
  if(backgroundPixelNumber > 0) {
    drawZone.classList.remove('ok');
    drawZone.classList.add('nok');
  } else {
    drawZone.classList.remove('nok');
    drawZone.classList.add('ok');
  }
}
            
          
!
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