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

              
                <!-- fork https://codepen.io/AlbertFeynman/pen/RBBKMe -->
<div id="app">
    <div class="scratch">
      <canvas class="scratch__canvas"></canvas>
      <img class="scratch__result" src="https://killacorp.com/wp-content/uploads/2020/04/bar2.png" alt="" />
      
    </div>
  </div>
              
            
!

CSS

              
                *{
  margin:0;
  padding:0;
}

html,body,#app{
  width: 100%;
  height: 100%;
  overflow: hidden;
}
body{
  background: #d3d3d3;
  background: linear-gradient(135deg, #cccccc 0%,#e1e1e1 51%,#d4d4d4 100%);
}
#app{
  display: flex;
  justify-content: center;
  align-items: center;
}
.scratch{
  position: relative;
  overflow: hidden;
  background: #252525;
  border-radius: 5px;
  box-shadow: -2px 2px 0px 0px #202020, 
    -10px 10px 40px 0px rgba(0, 0, 0, 0.3);
  transform: translate(0px, 0px) rotateX(30deg) rotateZ(-20deg);
  z-index: 2;
  
  cursor: pointer;
}
.scratch.animate{
  animation: introAnimate .8s;
}
.scratch .scratch__canvas{
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  max-width: 500px;
}
.scratch .scratch__result{
  visibility: hidden;
  width: 100%;
}
@keyframes introAnimate{
  from {
    transform: translate(0px, -100px) rotateX(10deg) rotateZ(-20deg);
    box-shadow: -2px 1px 0px 0px #202020, -40px 40px 40px 0 rgba(0, 0, 0, 0.2);
  }
  to {
    transform: translate(0px, 0px) rotateX(30deg) rotateZ(-20deg);
  }
}
              
            
!

JS

              
                // https://gist.github.com/Yaffle/1145197
/*
  convertPointFromPageToNode(element, event.pageX, event.pageY) -> {x, y}
  returns coordinate in element's local coordinate system (works properly with css transforms without perspective projection)
  convertPointFromNodeToPage(element, offsetX, offsetY) -> {x, y}
  returns coordinate in window's coordinate system (works properly with css transforms without perspective projection)
*/

(function () {
  "use strict";

  var I = new WebKitCSSMatrix();

  function Point(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }

  Point.prototype.transformBy = function (matrix) {
    var tmp = matrix.multiply(I.translate(this.x, this.y, this.z));
    return new Point(tmp.m41, tmp.m42, tmp.m43);
  };

  // new WebKitCSSMatrix(), new WebKitCSSMatrix(string)
  // WebKitCSSMatrix#m41, WebKitCSSMatrix#m42, WebKitCSSMatrix#m43
  // WebKitCSSMatrix#multiply, WebKitCSSMatrix#translate, WebKitCSSMatrix#inverse

  function getTransformationMatrix(element) {
    var transformationMatrix = I;
    var x = element;

    while (x != undefined && x !== x.ownerDocument.documentElement) {
      var computedStyle = window.getComputedStyle(x, undefined);
      var transform = computedStyle.transform || "none";
      var c = transform === "none" ? I : new WebKitCSSMatrix(transform);
      transformationMatrix = c.multiply(transformationMatrix);
      x = x.parentNode;
    }

    var w = element.offsetWidth;
    var h = element.offsetHeight;
    var i = 4;
    var left = +Infinity;
    var top = +Infinity;
    while (--i >= 0) {
      var p = new Point(i === 0 || i === 1 ? 0 : w, i === 0 || i === 3 ? 0 : h, 0).transformBy(transformationMatrix);
      if (p.x < left) {
        left = p.x;
      }
      if (p.y < top) {
        top = p.y;
      }
    }
    var rect = element.getBoundingClientRect();
    transformationMatrix = I.translate(window.pageXOffset + rect.left - left, window.pageYOffset + rect.top - top, 0).multiply(transformationMatrix);

    return transformationMatrix;
  }

  window.convertPointFromPageToNode = function (element, pageX, pageY) {
    return new Point(pageX, pageY, 0).transformBy(getTransformationMatrix(element).inverse());
  };
  
  window.convertPointFromNodeToPage = function (element, offsetX, offsetY) {
    return new Point(offsetX, offsetY, 0).transformBy(getTransformationMatrix(element));
  };

}());

// entry
// fork https://codepen.io/andreruffert/pen/pvqly

var Scratch = (function (){
  var BRUSH_B64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAxCAYAAABNuS5SAAAKFklEQVR42u2aCXCcdRnG997NJtlkk83VJE3apEma9CQlNAR60UqrGSqW4PQSO9iiTkE8BxWtlGMqYCtYrLRQtfVGMoJaGRFliijaViwiWgQpyCEdraI1QLXG52V+n/5nzd3ENnX/M8/sJvvt933/533e81ufL7MyK7NOzuXPUDD0FQCZlVn/+xUUQhkXHny8M2TxGsq48MBjXdAhL9/7YN26dd5nI5aVRrvEc0GFEBNKhbDjwsHh3qP/FJK1EdYIedOFlFAOgREhPlICifZDYoBjTna3LYe4xcI4oSpNcf6RvHjuAJRoVszD0qFBGmgMChipZGFxbqzQkJWVZUSOF7JRX3S4LtLTeyMtkkqljMBkPzHRs2aYY5PcZH/qLY1EIo18byQ6hBytIr3WCAXcV4tQHYvFxg3w3N6+Bh3OQolEoqCoqCinlw16JzTFJSE6PYuZKqvztbC2ex7bzGxhKu+rerjJrEEq+r9ieElJSXFDQ0Mh9zYzOzu7FBUWcO4Q9xbD6HYvhXhGLccVD5ZAPyfMqaioyOrBUgEv8FZXV8caGxtz8vLykhCWTnZIKmsKhUJnEYeKcKk2YYERH41G7UYnck1/WvAPOxsdLJm2+bEY0Ay0RNeqkytXQkoBZM4U5oOaoYSUkBGRtvnesrBZK4e4F6ypqSkuLy+v4KI99ZQxkfc6vZ4jNAl1wkbhG8LrhfNBCdkxmhYacvj/GOce+3K9MHHbDHUmicOufREELRIWch/DljzMsglutr+VIJO5KjGrVfZAnpF8mnCd8G5hrnC60Cl8T/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YtYNabh9EGik3Mkk+A9I8JTWoAiik/LEpzY8tY4uwWc4AJMjxQd8oXRHU8JqbW32orNyAiubZo0WR5wX9KyHrLpLD52nrxhFHa1CVV5w3081cRu/7BYichpEqfafA7/sCzhT7tVkhLZvhTeB8Gv1r6U+ty/gqtWHQCSNTcPOl9NmXM1S4hgRjBjjL1MdUJ8cx3uhe3d3dfh5Meb8qyKWsuJRidwtN/h20XEtxvTwya7tKncU8ACqmXVwLict5fy6TnFhra2uW7xT8dWk2BHptVBOx8GLKjo3g7bhrBQq1sdVsCvEkhLZIac1y/zmUSO0oO8fX/0P2Ub3cwaWpZSITnLnOpDlBWTIfMleJqFb10jXCBJUlMyORSIP14LhqNef6v/05bpZTdHulUyXKsufDNdRxZ4vIhSKwhQFG5vfLfcwZsx2X92Jhje8/P8OI+TK/oO+zeA84WTzkvI/6RuB3y6f68qf11xnyMiuzMms4178AwArmZmkkdGcAAAAASUVORK5CYII=';
  var distanceBetween = function(point1, point2) {
    var a = point2.x - point1.x;
    var b = point2.y - point1.y;
    return Math.sqrt((a*a) + (b*b));
  }
  var angleBetween = function(point1, point2) {
    return Math.atan2(point2.x - point1.x, point2.y - point1.y);
  }

  var AUTO_DRAW_POSITION = [{"x":253,"y":103.5},{"x":249,"y":111.5},{"x":248,"y":117.5},{"x":247,"y":122.5},{"x":251,"y":125.5},{"x":258,"y":128.5},{"x":263,"y":131.5},{"x":264,"y":133.5},{"x":264,"y":136.5},{"x":263,"y":140.5},{"x":260,"y":145.5},{"x":257,"y":150.5},{"x":254,"y":153.5},{"x":251,"y":156.5},{"x":251,"y":157.5},{"x":250,"y":157.5},{"x":250,"y":158.5},{"x":249,"y":158.5}];

  function Scratch(canvas/*HTMLCanvasElement*/, coverImgURL/*string*/, completeCallback/*Function*/){
    var self = this;
    this.canvas = canvas;
    this.autoDrawing = false;
    this.completeCallback = completeCallback;
    this.supportTouch = ('ontouchstart' in window);
    this.isDrawing = false;
    this.lastPoint = {x:0,y:0};
    this.context = canvas.getContext('2d');
    this.handleMouseDown = this.handleMouseDown.bind(this);
    this.handleMouseMove = this.handleMouseMove.bind(this);
    this.handleMouseUp = this.handleMouseUp.bind(this);
    this.brushImg = new Image();
    this.brushImg.src = BRUSH_B64;
    // width 80, height 50
    /* this.brushImg.onload = ()=>{
      console.log(this.brushImg.width);
      console.log(this.brushImg.height);
    } */

    var element = document.querySelector('.scratch');
    var img = new Image();
    this.img = img;
    img.src = coverImgURL;
    img.onload = function (){
      element.classList.add('animate');
      document.querySelector('.scratch__result').style.visibility = 'visible';
      var ctx = self.context;
      var w = img.width;
      var h = img.height;
      
      self.canvas.width = w;
      self.canvas.height = h;
      ctx.drawImage(img, 0, 0);

      element.addEventListener('touchstart', self.handleMouseDown);
      window.addEventListener('touchmove', self.handleMouseMove);
      window.addEventListener('touchend', self.handleMouseUp);	
      element.addEventListener('mousedown', self.handleMouseDown);
      window.addEventListener('mousemove', self.handleMouseMove);
      window.addEventListener('mouseup', self.handleMouseUp);


      self.autoDraw(AUTO_DRAW_POSITION);
    };
  }
  Scratch.prototype.getMousePosition = function (event) {
    const pageScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    const pageScrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
    const evt = this.supportTouch ? event.touches[0] : event;
    const rect = this.canvas.getBoundingClientRect();
    if(window.convertPointFromPageToNode){
      return window.convertPointFromPageToNode(document.querySelector('.scratch'), evt.pageX, evt.pageY);
    }
    return {
      x: evt.pageX - rect.left - pageScrollLeft,
      y: evt.pageY - rect.top - pageScrollTop,
    };
  };
  Scratch.prototype.handleMouseDown = function (event){
    event.preventDefault();
    this.lastPoint = this.getMousePosition(event);
    this.isDrawing = true;
  }
  Scratch.prototype.handleMouseMove = function (event){
    if(!this.isDrawing || this.autoDrawing){
      return;
    }
    var position = this.getMousePosition(event);
    this.draw(position);
    var progress = this.getFilledInPixels(32);
    if(progress > 0.5 && this.completeCallback){
      this.completeCallback.call(this);
    } 
  }
  Scratch.prototype.autoDraw = function (){
    this.autoDrawing = false;
    this.lastPoint = positoins[0];
    var self = this;
    var index = 0;
    var fps = 40;
    var now;
    var then = Date.now();
    var interval = 1000/fps;
    var delta;
    var animationFrameID = -1;
    var d = function (){
      animationFrameID = requestAnimationFrame(d);
      now = Date.now();
      delta = now - then;
      if (delta > interval) {
        then = now - (delta % interval);
        self.draw(positoins[index]);
        index++;
        if(index >= positoins.length-1){
          self.autoDrawing = false;
          cancelAnimationFrame(animationFrameID);
        }
      }
    }
    setTimeout(d, 1500);
  }
  Scratch.prototype.draw = function (position){
    var dist = distanceBetween(this.lastPoint, position);
    var angle = angleBetween(this.lastPoint, position);
    var ctx = this.context;
    var x,y;
    for (var i = 0; i < dist; i += 1) {
      x = this.lastPoint.x + (Math.sin(angle) * i);
      y = this.lastPoint.y + (Math.cos(angle) * i);
      ctx.globalCompositeOperation = 'destination-out';
      ctx.drawImage(this.brushImg, x - 40, y - 25)
    }
    this.lastPoint = position;
  }
  Scratch.prototype.handleMouseUp = function (event){
    this.isDrawing = false;
  }
  Scratch.prototype.getFilledInPixels = function(stride/*number*/) {
    if (!stride || stride < 1) { 
      stride = 1; 
    }
    var pixels = this.context.getImageData(0, 0, this.canvas.width, this.canvas.height);
    var data = pixels.data;
    var length = data.length;
    var total = (length / stride);
    var count = 0;
    for(var i = count = 0; i < length; i += stride) {
      if (parseInt(data[i]) === 0) {
        count++;
      }
    }
    
    return count / total;
  }
  Scratch.prototype.destory = function (){
    this.canvas.removeEventListener('mousedown', this.handleMouseDown);
    this.canvas.removeEventListener('mousemove', this.handleMouseMove);
    this.canvas.removeEventListener('mouseup', this.handleMouseUp);
    this.canvas.removeEventListener('touchstart', this.handleMouseDown);
    this.canvas.removeEventListener('touchmove', this.handleMouseMove);
    this.canvas.removeEventListener('touchend', this.handleMouseUp);
    this.canvas.parentNode.removeChild(this.canvas);
  }
  return Scratch;
})();


var app = new Scratch(document.querySelector('.scratch__canvas'), 'https://killacorp.com/wp-content/uploads/2020/07/scratchcard.jpg', function (){
  console.log('scratch complete');
})
              
            
!
999px

Console