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.

            
              <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>

            
          
!
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.

Console