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

              
                <html>
<body>
  <style>
    example-canvas-component {
      width: 100px;
      height: 100px;
      border: 1px solid black;
    }
  </style>
  <example-canvas-component></example-canvas-component>
  <script type="text/javascript">
    // This is an 'object' that is capable of updating it's internal state based upon
    // provided environmental conditions and is also capable of drawing itself.
    // Notice this is not a web component but rather just a plain old ES6 class.
    class ExampleLine {
      constructor({context, contextWidth, contextHeight, color = 'rgb(0,0,0)', thickness = 2, speed = 2, sensitivity = 0.1, minOpacity = 0.2}) {
        this.context = context;
        this.contextWidth = contextWidth;
        this.contextHeight = contextHeight;
        this.color = color;
        this.thickness = thickness;
        this.speed = speed;
        this.sensitivity = sensitivity;
        this.minOpacity = minOpacity;
        this.alpha = 1;

        // Initialize the internal state of the object.
        // Positions should be stored as scaled values. 0 indicates the minimum width or height
        // and 1 indicates the maximum width or height.
        this.targets = [
          { p1: { x: 0, y: 0 },
            p2: { x: 1, y: 1 } },
          { p1: { x: 0, y: 1 },
            p2: { x: 1, y: 0 } }
        ];

        this.targetIndex = 0;
        this.p1 = this.targets[0].p1;
        this.p2 = this.targets[0].p2;
      }

      get previousTarget() {
        return this.targetIndex === 0 ? this.targets[this.targets.length - 1] : this.targets[this.targetIndex - 1];
      }

      get target() {
        return this.targets[this.targetIndex];
      }

      // Only convert from scaled positions to absolute positions when they are needed to draw.
      // Internal state should be scaled positions so that the client can resize the component.
      get pos() {
        return {
          p1: { x: this.contextWidth  * this.p1.x,
                y: this.contextHeight * this.p1.y },
          p2: { x: this.contextWidth  * this.p2.x,
                y: this.contextHeight * this.p2.y }
        }
      }

      updateTarget() {
        if (this.targetIndex === this.targets.length - 1) {
          this.targetIndex = 0;
        } else {
          this.targetIndex++;
        }
      }

      hasReachedTarget() {
        // Move algorithms into utility methods.
        return distanceBetween(this.p1, this.target.p1) < this.sensitivity &&
               distanceBetween(this.p2, this.target.p2) < this.sensitivity;
      }

      // Update the internal state of this object
      update(mousePosition = { x: 0, y: 0 }) {
        if (this.hasReachedTarget()) this.updateTarget();
        
        // Update internal state based upon environment information
        this.alpha =  ((mousePosition.x / this.contextWidth) * (1 - this.minOpacity)) + this.minOpacity;

        // Move algorithms into utility methods.
        this.p1 = easePoint(this.previousTarget.p1, this.p1, this.target.p1, this.speed);
        this.p2 = easePoint(this.previousTarget.p2, this.p2, this.target.p2, this.speed);
      }

      // Drawing or updating objects might require environment information. In thise case
      // we need a mouse position in order to draw the line.
      draw() {
        this.context.beginPath();
        this.context.lineWidth = this.thickness;
        this.context.strokeStyle = this.color;
        this.context.globalAlpha = this.alpha;
        this.context.lineTo(this.pos.p1.x, this.pos.p1.y);
        this.context.lineTo(this.pos.p2.x, this.pos.p2.y);
        this.context.stroke();
      }
    }

    // A web component that creates a canvas and acts as the 'stage' in which various
    // 'objects' can update draw themselves.
    class ExampleCanvasComponent extends HTMLElement {
      constructor() {
        // Here we simply create a canvas and apply default styling. We will manually
        // Update the size of the canvas to fill the element through JavaScript.
        // By doing this we will allow the client code to determine the size of the component
        // And we will do all of our calculations as scaled values
        super();
        this.shadow = this.attachShadow({mode: 'open'});
        this.shadow.innerHTML = `
          <style media="screen">
            :host {
              cursor: pointer;
              display: block;
            }
          </style>
          <canvas></canvas>
        `;
      }

      connectedCallback() {
        this.canvas = this.shadow.querySelector('canvas');
        this.context = this.canvas.getContext('2d');
        this.mousePosition = {x: 0, y: 0};

        // Update canvas to fill the space of the component.
        this.canvas.width = this.clientWidth;
        this.canvas.height = this.clientHeight;
        this.objects = []
        this.hovering = false;

        // Make sensible default values.
        // These defaults also modify the client provided values so that providing "1" acts as the anchor value.
        // Values above 1 will be more than the default and values below 1 will be less than the default.
        this._defaultSpeed = 0.1;
        this._defaultLineThickness = 2;

        // Provide configuration options through attributes on the element.
        this.lineThickness = (parseFloat(this.getAttribute('line-thickness')) * this._defaultLineThickness) || this._defaultLineThickness;
        this.speed = (parseFloat(this.getAttribute('speed')) * this._defaultSpeed) || this._defaultSpeed;

        // Set the scene by creating objects
        this.objects.push(new ExampleLine({
          context: this.context,
          contextWidth: this.canvas.width,
          contextHeight: this.canvas.height,
          thickness: this.lineThickness,
          speed: this.speed
        }));

        // The component is resopobsible for maintaining environment information.
        this.canvas.addEventListener('mousemove', event => {
          let rect = this.canvas.getBoundingClientRect();
          this.mousePosition = {
            x: event.clientX - rect.left,
            y: event.clientY - rect.top
          };
        }, false);
        this.canvas.addEventListener("mouseenter", event => this.hovering = true);
        this.canvas.addEventListener("mouseleave", event => this.hovering = false);

        // Run the animation loop.
        let animate = () => {
          requestAnimationFrame(animate);
          // Remember to only perform updates if they are needed.
          // In this case we only want to perform the animation if the mouse is hovering.
          if (this.hovering) {
            this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
            this.doAnimate();
          }
        }
        animate();
      }

      doAnimate() {
        this.update();
        this.draw();
      }

      // Update logic for each object in the canvas should be abstracted away by
      // a different class. Current environment information must be passed in.
      update() {
        this.objects.forEach(object => object.update(this.mousePosition));
      }

      // Drawing logic for each object in the canvas should be abstracted away by
      // a different class. Current environment information must be passed in.
      draw() {
        this.objects.forEach(object => object.draw());
      }
    }

    customElements.define('example-canvas-component', ExampleCanvasComponent);

    /** @function distanceBetween
     *  @param p1 An object with an x and a y value.
     *  @param p2 An object with an x and a y value.
     *  The distance between two points. p1 and p2 should each have an x and y property.
     */
    function distanceBetween(p1, p2) {
      return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
    }

    /** @function easePoint
     *  @param p0 The original starting point. An object with an x and a y value.
     *  @param p1 The current point. An object with an x and a y value.
     *  @param p2 The destination. An object with an x and a y value.
     *  @param speedScale A value to scale the movement by.
     *  @returns a new point moved from p1 towards p2 with an easing function applied
     *  based upon the original starting point p0. The speed of the movement is scalled by speedScale.
     */
    function easePoint(p0, p1, p2, speedScale) {
      var xDiff = p2.x - p1.x;
      var yDiff = p2.y - p1.y;
      var xTotal = Math.abs(p2.x - p0.x);
      var yTotal = Math.abs(p2.y - p0.y);

      var newX;
      if (xTotal > 0) {
        var xSpeedAdjust = Math.pow(Math.abs(Math.sin((Math.abs(xDiff) / xTotal) * Math.PI)), 1/3);
        var xMove = xDiff * speedScale * xSpeedAdjust;
        newX = p1.x + xMove;
      } else {
        newX = p2.x;
      }

      var newY;
      if (yTotal > 0) {
        var ySpeedAdjust = Math.pow(Math.abs(Math.sin((Math.abs(yDiff) / yTotal) * Math.PI)), 1/3);
        var yMove = yDiff * speedScale * ySpeedAdjust;
        newY = p1.y + yMove;
      } else {
        newY = p2.y;
      }

      return {
        x: newX,
        y: newY
      };
    }
  </script>
</body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console