Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                  <body>

    <!-- MAIN CONTAINER OF SCRATCH FEATURE -->
    <div class="scratch-container mt-3">
      <div id="scratchBase" class="p-5">
        <form>
  <div class="mb-3">
    <label for="exampleInputEmail1" class="form-label">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
    <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
  </div>
  <div class="mb-3">
    <label for="exampleInputPassword1" class="form-label">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1">
  </div>
  <div class="mb-3 form-check">
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Check me out</label>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>
      </div>
      <!-- IMAGE CANVA TO SCRATCH BEFORE SHOWING OF BASE CONTENT -->
      <canvas id="scratch" src="https://images.pexels.com/photos/4807213/pexels-photo-4807213.jpeg?auto=compress&cs=tinysrgb&w=1600&lazy=load"></canvas>
      <!-- END OF IMAGE CANVA TO SCRATCH BEFORE SHOWING OF BASE CONTENT -->

    </div>
    <!-- END OF MAIN CONTAINER OF SCRATCH FEATURE -->


    <!-- MODAL POPUP -->
    <div class="modal" tabindex="-1">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title">Modal title</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            <p>Modal body text goes here.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
    <!-- END OF MODAL POPUP -->
    
    <div class="state d-none">
      <dd id="gray-count">?</dd>
    </div>

  </body>
              
            
!

CSS

              
                #scratchBase {
    text-align: center;
    position: relative;
    -webkit-user-select: unset;
   /* Safari */
    -ms-user-select: unset;
   /* IE 10 and IE 11 */
    user-select: unset;
   /* Standard syntax */
   opacity: 0;
}
#scratch {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    z-index: 9;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    position: absolute;
}
.scratch-container {
    width: fit-content;
    margin: 0 auto 50px;
    display: flex;
    justify-content: center;
    align-items:center;
    border: 4px solid red;
    position: relative;
}
              
            
!

JS

              
                (function() {
    'use strict';
    var canvas = document.getElementById('scratch'),
        context = canvas.getContext('2d');
    // default value
    context.globalCompositeOperation = 'source-over';
  
    // Getting Scratch Container Dimension
    var scratchContainer = $('#scratchBase');
    var scratchInnerWidth = scratchContainer.outerWidth();
    var scratchInnerHeight = scratchContainer.outerHeight();
    var scratchPadding = (scratchContainer.innerWidth() - scratchContainer.width()) /2;
    drawImage();
  
    //----------------------------------------------------------------------------
    // fill rectangle
    function drawImage() {
      $('#scratch').attr({
          'width': scratchInnerWidth +'px',
          'height': scratchInnerHeight +'px'
     })

      context.beginPath();
      context.fillStyle = '#111';
      context.fillRect(0,0,scratchInnerWidth,scratchInnerHeight);
      context.fill();

      var bgImage = $('#scratch').attr('src');

      if (bgImage!=undefined) {
        var background = new Image();
            background.crossOrigin = "anonymous";
            background.src = bgImage;
            background.onload = function() {
            context.drawImage(background,0,0,scratchInnerWidth,scratchInnerHeight);   
        }
      }
      
    }
  
    
    //----------------------------------------------------------------------------
    var isDrag = false;
    function clearArc(x, y) {
        context.globalCompositeOperation = 'destination-out';
        context.beginPath();
        context.arc(x, y, 23, 0, Math.PI * 2, false);
        context.fill();
    }
    canvas.addEventListener('mousedown', function(event) {
        isDrag = true;
        clearArc(event.offsetX, event.offsetY);
        judgeVisible();
    }, false);
    canvas.addEventListener('mousemove', function(event) {
        if (!isDrag) {
            return;
        }
        clearArc(event.offsetX, event.offsetY);
      // console.log(event.offsetX, event.offsetY)
        judgeVisible();
    }, false);
    canvas.addEventListener('mouseup', function(event) {
        isDrag = false;
    }, false);
    canvas.addEventListener('mouseleave', function(event) {
        isDrag = false;
    }, false);
    //----------------------------------------------------------------------------
    canvas.addEventListener("touchstart", (event) => {
        if (event.targetTouches.length !== 1) {
            return;
        }
        event.preventDefault();
        isDrag = true;
        var bcr = event.target.getBoundingClientRect();
        clearArc(event.targetTouches[0].clientX - bcr.x, event.targetTouches[0].clientY - bcr.y);
        judgeVisible();
    }, false);
    canvas.addEventListener("touchmove", (event) => {
        if (!isDrag || event.targetTouches.length !== 1) {
          console.log(event.targetTouches.length)
          return;
        }
    event.preventDefault();
    var bcr = event.target.getBoundingClientRect();
    // console.log(event.targetTouches[0].clientX, event.targetTouches[0].clientY)
    clearArc(event.targetTouches[0].clientX - bcr.x, event.targetTouches[0].clientY - bcr.y);
    judgeVisible();
  }, false);
    canvas.addEventListener("touchend", (event) => {
        isDrag = false;
        console.log(isDrag)
    }, false);
  //---------------------------------------------------------------------------- 
   function judgeVisible() {
        var imageData = context.getImageData(scratchPadding,scratchPadding,parseInt(scratchInnerWidth) - (parseInt(scratchPadding)*2),parseInt(scratchInnerHeight) -(parseInt(scratchPadding)*2)),
            pixels = imageData.data,
            result = {},
            i, len;
        // count alpha values
        for (i = 3, len = pixels.length; i < len; i += 4) {
            result[pixels[i]] || (result[pixels[i]] = 0);
            result[pixels[i]]++;
        }
        document.getElementById('gray-count').innerHTML = result[255];
        document.getElementById('scratchBase').style.setProperty('opacity', '0.6')
         if (document.getElementById('gray-count').innerHTML <= 500 ) {
            $(".modal").modal().show();
            document.getElementById('scratchBase').style.setProperty('z-index', '999')
            document.getElementById('scratchBase').style.setProperty('opacity', '1')
        }
        else if (document.getElementById('gray-count').innerHTML <= 10000 ) {
            document.getElementById('scratchBase').style.setProperty('opacity', '0.9')
        }
        
    }
  
    document.addEventListener('DOMContentLoaded', judgeVisible, false);
  
  }());
  
 $('body').on('click', '[data-bs-dismiss]', function() {
        $(".modal").modal().hide();
 })
              
            
!
999px

Console