Pen Settings

HTML

CSS

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

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

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.

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

              
                <div class="titles">
  <h1>Floating Tags</h1>
  <h3 class="device_pixel_ratio">...</h3>
</div>
<canvas class="canvasElm"></canvas>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  border: 0;
}

html {
  box-sizing: border-box;
  font-size: 62.5%; /* reseting base font-size to 10px */
  scroll-behavior: smooth;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}

* {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-size: 1.6em; /* base font size is 16px */
  line-height: 1.4;
  font-family: -apple-system, sans-serif, BlinkMacSystemFont, "Segoe UI",
    Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji",
    "Segoe UI Symbol";
  background-color: white;
  color: rgba(0, 0, 0, 0.9);
  min-height: 100vh;
  /* mobile viewport bug fix */
  min-height: -webkit-fill-available;
}

/* ———————————————————————— */

.canvasElm {
  box-shadow: 0 0 40px 1px rgba(0, 0, 0, 0.1);
  background: #ffffff;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.titles {
  position: absolute;
  top: 24px;
  left: 24px;
  color: rgba(0, 0, 0, 1);
  max-width: 240px;
  line-height: 1.2;
  z-index: 2;
  pointer-events: none;
}
.titles h1 {
  font-size: 24px;
  margin-bottom: 8px;
}
.titles h3 {
  font-size: 16px;
  font-weight: 400;
  opacity: 0.6;
}

              
            
!

JS

              
                const canvasElm = document.querySelector(".canvasElm");

const deviceScale = window.devicePixelRatio;
console.log("Device pixel ratio is", deviceScale);

const devicePixelRatioTag = document.querySelector(".device_pixel_ratio");
devicePixelRatioTag.innerHTML = `Device pixel ratio is → ${deviceScale}`;

function setCanvasSize() {
  canvasElm.width = window.innerWidth * deviceScale;
  canvasElm.height = window.innerHeight * deviceScale;
}
setCanvasSize();

// Resize canvas on window resize
window.addEventListener("resize", () => {
  setCanvasSize();
});

let colors = [
  "0,183,172",
  "27,180,24",
  "37,166,206",
  "147,62,212",
  "222,58,22",
  "222,162,9",
  "234,65,176"
];
let tags = [
  "Vue.js",
  "Javascript",
  "HTML",
  "Firebase",
  "Canvas API",
  "CSS",
  "SASS",
  "Git"
];

function randomBetween(min, max) {
  return Math.random() * (max - min) + min;
}

let ctx = canvasElm.getContext("2d");

class Tag {
  constructor(
    x,
    y,
    dx,
    dy,
    padding,
    radius,
    text,
    fontSize,
    textColor,
    bgColor
  ) {
    this.x = x;
    this.y = y;
    this.dx = dx;
    this.dy = dy;
    this.padding = padding;
    this.radius = radius;
    this.text = text;
    this.fontSize = fontSize;
    this.textWidth;
    this.width;
    this.height;
    this.textColor = textColor;
    this.bgColor = bgColor;
  }

  prepareText() {
    ctx.font = `${this.fontSize}px Arial`;
    this.textWidth = ctx.measureText(this.text).width;
    this.width = this.textWidth + this.padding * 2;
    this.height = this.fontSize + this.padding * 2;
  }

  createRoundedRect() {
    ctx.beginPath();
    ctx.moveTo(this.x + this.radius, this.y);

    // top side
    ctx.lineTo(this.x + this.width - this.radius, this.y);
    ctx.quadraticCurveTo(
      this.x + this.width,
      this.y,
      this.x + this.width,
      this.y + this.radius
    );

    // right side
    ctx.lineTo(this.x + this.width, this.y + this.height - this.radius);
    ctx.quadraticCurveTo(
      this.x + this.width,
      this.y + this.height,
      this.x + this.width - this.radius,
      this.y + this.height
    );

    // bottom side
    ctx.lineTo(this.x + this.radius, this.y + this.height);
    ctx.quadraticCurveTo(
      this.x,
      this.y + this.height,
      this.x,
      this.y + this.height - this.radius
    );

    // left side
    ctx.lineTo(this.x, this.y + this.radius);
    ctx.quadraticCurveTo(this.x, this.y, this.x + this.radius, this.y);

    ctx.closePath();
    ctx.fillStyle = this.bgColor;
    ctx.fill();
  }

  render() {
    this.prepareText();
    this.createRoundedRect();
    ctx.fillStyle = this.textColor;
    ctx.fillText(
      this.text,
      this.x + this.padding,
      this.y + this.fontSize / 1.2 + this.padding
    ); // fontSize / line-height + padding
    this.update();
  }

  update() {
    this.x += this.dx;
    this.y += this.dy;

    // -- Detecting left & right sides
    if (this.x + this.width >= canvasElm.width || this.x <= 0) {
      this.dx *= -1;
      let randomColor = colors[Math.floor(randomBetween(0, colors.length))];
      this.bgColor = `rgba(${randomColor}, 0.04)`;
      this.textColor = `rgba(${randomColor}, 1)`;
    }
    // Stop tags from being generating outside the right side of the canvas
    if (this.x + this.width >= canvasElm.width) {
      this.x = canvasElm.width - this.width;
    }

    // Prevent tags from getting stuck on left side when screen is too narrow
    if (this.x <= 0) {
      this.x += 1;
    }

    // -- Detecting top & bottom sides
    if (this.y + this.height >= canvasElm.height || this.y <= 0) {
      this.dy *= -1;
      let randomColor = colors[Math.floor(randomBetween(0, colors.length))];
      this.bgColor = `rgba(${randomColor}, 0.06)`;
      this.textColor = `rgba(${randomColor}, 1)`;
    }
    // Stop tags from being generated outside the bottom side of the canvas
    if (this.y + this.height >= canvasElm.height) {
      this.y = canvasElm.height - this.height;
    }
  }
}

let tagsArray = [];
function getTags() {
  tags.forEach((tag, index) => {
    let x = randomBetween(0, canvasElm.width);
    let y = randomBetween(0, canvasElm.height);
    let dx;
    let dy;
    if (index % 2) {
      // every other tag would move the opposie direction - This is needed to avoid getting 0
      dx = randomBetween(-1, -0.4) * deviceScale;
      dy = randomBetween(-1, -0.4) * deviceScale;
    } else {
      dx = randomBetween(0.4, 1) * deviceScale;
      dy = randomBetween(0.4, 1) * deviceScale;
    }
    // TODO: add rule to prevent dy/dx of 0
    let padding = 12 * deviceScale;
    let radius = 16 * deviceScale;
    let fontSize = 24 * deviceScale;
    let textColor = "rgba(0,0,0,0.4)";
    let bgColor = "rgba(0,0,0,0.04";
    let tagElm = new Tag(
      x,
      y,
      dx,
      dy,
      padding,
      radius,
      tag,
      fontSize,
      textColor,
      bgColor
    );
    tagsArray.push(tagElm);
  });
}
getTags();

function animate() {
  ctx.clearRect(0, 0, canvasElm.width, canvasElm.height);
  tagsArray.forEach((tag) => {
    tag.render();
  });
  requestAnimationFrame(animate);
}
animate();

              
            
!
999px

Console