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

              
                <div class="wrapper">
  
 <div class="innerWrapper">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 158.82 186.47"><title>Tamag</title><g id="outline"><path id="main-tamago" d="M23.5,104.55l.88,14.21a55.29,55.29,0,0,0,4,17.52l5.16,12.64a55.37,55.37,0,0,0,12.88,19L50.68,172a55.08,55.08,0,0,0,18.61,11.78l11.36,4.33a55.39,55.39,0,0,0,19.65,3.61h5.25A55.3,55.3,0,0,0,121,189.45l7.79-2.27A55.37,55.37,0,0,0,146,178.72l3.91-2.87a55.31,55.31,0,0,0,13.41-14l5.37-8.11a55,55,0,0,0,6.78-14.38l4-13a55.35,55.35,0,0,0,2.22-11.55l.38-4.62a55,55,0,0,0-.34-12.23L180,85.52A56.06,56.06,0,0,0,177.42,75l-5.07-14.56a55.22,55.22,0,0,0-4.4-9.57L163.37,43A55.77,55.77,0,0,0,157,34.12l-5.61-6.34a55,55,0,0,0-12.53-10.53L133.5,14a55.51,55.51,0,0,0-10.07-4.84l-1.7-.62a55.31,55.31,0,0,0-18.81-3.3h0A55,55,0,0,0,86.61,7.67l-.42.13a55.32,55.32,0,0,0-13,5.93L67.68,17.2a55.08,55.08,0,0,0-11.05,9.08l-6.83,7.3a55.77,55.77,0,0,0-7.9,10.8L34.44,57.73A55.17,55.17,0,0,0,29,71.62L25,88.1A55.22,55.22,0,0,0,23.5,104.55Z" transform="translate(-23.4 -5.2)" style="fill:#ff7600"/></g><g id="light"><path d="M138.18,42.31l-1.7,13.12,16.93,12.48-2.5,18.59,4.81,12.85a5.49,5.49,0,0,0,3.82,3.37h0a5.42,5.42,0,0,0,6.08-2.79l2-3.87a5.53,5.53,0,0,0,.59-2l1.23-14a5.58,5.58,0,0,0-.07-1.47l-2.5-13.5a5.57,5.57,0,0,0-.49-1.44l-4.92-9.84a5.36,5.36,0,0,0-.69-1.06l-9-10.75a5.4,5.4,0,0,0-1.36-1.16l-4-2.45A5.43,5.43,0,0,0,138.18,42.31Z" transform="translate(-23.4 -5.2)" style="fill:#fff;opacity:0.36"/></g><g id="darkshadowy"><path d="M76.27,9.36" transform="translate(-23.4 -5.2)" style="fill:none;stroke:red;stroke-miterlimit:10"/><g style="opacity:0.21"><path d="M73.22,13.73,67.68,17.2a55.08,55.08,0,0,0-11.05,9.08l-6.83,7.3a55.77,55.77,0,0,0-7.9,10.8L34.44,57.73A55.17,55.17,0,0,0,29,71.62L25,88.1a55.22,55.22,0,0,0-1.47,16.45l.88,14.21a55.29,55.29,0,0,0,4,17.52l5.16,12.64a55.37,55.37,0,0,0,12.88,19L50.68,172a55,55,0,0,0,13.23,9.39,5.21,5.21,0,0,0,.89-3.91c-.32-2.44.83-10.25-9.12-18.19-9.15-7.3-11.92-8.62-14.71-13.32-2.29-3.87-3-7.29-4.16-13.32a86.46,86.46,0,0,1-1.39-16.93A126.47,126.47,0,0,1,36,101c.37-3.78.77-6,4.16-22.2,3.14-15,3.39-16,4.16-18,1-2.51,3.41-7.22,8.33-16.65,2.73-5.23,3.79-7.09,5.55-9.43a58.59,58.59,0,0,1,8-8.6c3.88-3.43,4.39-3,6.38-5.27.76-.85,4.84-5.57,5.06-9.51C76.15,12,74.66,12.83,73.22,13.73Z" transform="translate(-23.4 -5.2)" style="fill:#150d00"/></g></g><g id="inner-outline"><polygon points="36.5 79.91 24.84 109.33 36.5 126.81 34 136.52 47.6 147.34 62.86 142.9 76.45 154 93.66 143.46 114.75 147.06 125.84 114.6 133.34 106 122.52 84.08 124.73 64.65 108.92 52.44 96.99 59.1 70.35 50.22 55.09 57.72 38.44 56.05 36.5 79.91" style="fill:#fdffdf"/></g></svg>

<canvas class="cnvs"></canvas>

<button class="btn btn-1"></button>
<button class="btn btn-2"></button>
<button class="btn btn-3"></button>
  
  </div>
</div>
              
            
!

CSS

              
                body {
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100vh;
}
.wrapper {
  width: 100%;
  height: 100%;
  margin-top: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.innerWrapper {
  width: 250px;
  position: relative;
}

svg {
  width: 250px;
}

.cnvs {
  background-color: rgba(60, 75, 60, 0.3);
  position: absolute;
  top: 108px;
  left: 70px;
  border-radius: 10px;
  box-shadow: 5px 4px 0px 1px rgba(40,55,40, 0.4) inset;
}

.btn-1 {
  position: absolute;
  top: 257px;
  left: 80px;
}

.btn-2 {
  position: absolute;
  top: 265px;
  left: 120px;
}

.btn-3 {
  position: absolute;
  top: 257px;
  left: 160px;
}


button:focus {outline:0;}

.btn {
  border: none;
  background-color: #fdffdf;
  width: 20px;
  height: 20px;
  border-radius: 100%;
  box-shadow: 1px 1px 0px 2px #fdffdf44;
}

.btn:active {
  box-shadow: -0.5px -0.5px 0px 2px #00000033;
}
              
            
!

JS

              
                const cvs = document.querySelector(".cnvs");
const size = 110;
cvs.width = size;
cvs.height = size;
const ctx = cvs.getContext("2d");
const pxSize = 2;
const defaultColor = "rgb(60, 75, 60)";
const sleepingCat = `
xx...xx........x
x.xxx.x........x
x.....x.......x.
x.x.x.xxxxx..x..
x.....x....xxx..
.x...x.....x....
..xxxx.....x....
....xxxxxxx.....
`;

const standingCatToLeft = `
xx...xx........x
x.xxx.x........x
x.....x.......x.
x.x.x.xxxxx..x..
x.....x....xxx..
.x...x.....x....
..xxxx.....x....
....xxxxxxx.....
.....x.x.xxx....
.....x.x.x.x....
`;

const standingCatToLeftPose2 = `
xx...xx........x
x.xxx.x........x
x.....x.......x.
x.x.x.xxxxx..x..
x.....x....xxx..
.x...x.....x....
..xxxx.....x....
....xxxxxxx.....
....x.x.xxx.....
...x..x.x..x....
`;

const sleepZSml = `
xx
.x.
.xx
`;

const sleepZMed = `
xxx.
..x.
.x..
.xxx
`;

const sleepZLarge = `
xxxx.
..x..
.x...
.xxxx
`;

let state = {
  type: "walk",
  x: 0
};

function drawArt(px, py, str, flip, col = "rgb(60, 75, 60)") {
  const lines = str.split("\n");
  for (let y = 0; y < lines.length; y++) {
    let chars = lines[y].split("");
    if (flip) chars.reverse();
    for (let x = 0; x < chars.length; x++) {
      if (chars[x] === "x") drawRect(x + px, y + py, col, true);
    }
  }
}

function drawRect(x, y, col = "black", fill = false) {
  if (fill) {
    ctx.fillStyle = col;
    ctx.fillRect(x * pxSize, y * pxSize, pxSize, pxSize);
    return;
  }
  ctx.strokeStyle = col;
  ctx.fillStyle = "";
  ctx.beginPath();
  ctx.lineWidth = "1";
  ctx.rect(x * pxSize, y * pxSize, pxSize, pxSize);
  ctx.stroke();
}

function clear() {
  ctx.clearRect(0, 0, size, size);
  // ctx.fillRect(0, 0, size, size, 'rgba(0,0,0,0)');
}

function drawGrid() {
  let gridRows = Math.floor(size / pxSize);
  for (let i = 0; i < gridRows; i++) {
    for (let j = 0; j < gridRows; j++) {
      drawRect(j * 2, i * 2, "blue", false);
    }
  }
}

refresh();

function refresh() {
  clear();
  switch (state.type) {
    case "sleep":
      {
        sleepAnimation();
      }
      break;
    case "stand":
      {
        standAnimation();
      }
      break;
    case "walk":
      {
        walkAnimation();
      }
      break;
    default:
  }
}

function sleepAnimation() {
  if (state.t === undefined) state.t = 0;
  drawArt(20, 32, sleepingCat);
  if (state.t >= 1) drawArt(28, 27, sleepZSml);
  if (state.t >= 2) drawArt(32, 22, sleepZMed);
  if (state.t >= 3) drawArt(36, 18, sleepZLarge);
  state.t = (state.t + 1) % 4;
  if (Math.random() < 0.1) {
    state = { type: "walk", x: 0 };
  }
  setTimeout(refresh, 500);
}

function standAnimation() {
  if (state.t === undefined) state.t = 0;
  if (state.t < 4) drawArt(20 + state.x, 30, standingCatToLeft, true);
  else drawArt(20 + state.x, 30, standingCatToLeftPose2, true);
  state.t++;
  if (state.t > 8) state.t -= 8;
  state.x += 1;
  setTimeout(refresh, 50);
}

function walkAnimation() {
  if (state.t === undefined) state.t = 0;
  if (state.dir === undefined) state.dir = "left";
  const flip = state.dir === "right";
  if (state.t < 4) drawArt(20 + state.x, 30, standingCatToLeft, flip);
  else drawArt(20 + state.x, 30, standingCatToLeftPose2, flip);
  state.t++;
  if (state.t > 8) state.t -= 8;
  state.x += state.dir === "left" ? -1 : 1;

  if (state.dir === "left" && state.x < -10) state.dir = "right";
  if (state.dir === "right" && state.x > 10) state.dir = "left";

  if (state.x === 0) {
    if (Math.random() < 0.5) state = { type: "sleep" };
  }

  setTimeout(refresh, 100);
}

var elem;
var colorT;

function easeInOut(t) {
  const sqt = t ** 2;
  return sqt / (2.0 * (sqt - t) + 1.0);
}

var colorT = 0;
var elem;
function setColor() {
  if (colorT === undefined) colorT = 0;
  if (!elem) elem = document.querySelector("#main-tamago");
  const blueRGB = [98, 144, 241];
  const orangeRGB = [241, 195, 98];
  let t = colorT;
  const incAm = 0.05;
  if (state.type === "sleep") colorT = Math.min(t + incAm, 1);
  else colorT = Math.max(t - incAm, 0);
  t = easeInOut(t);
  const rgbOut = colorTweenRGB(orangeRGB, blueRGB, t);
  if (elem) elem.style.fill = `rgb(${rgbOut.join(",")})`;
  document.body.style.backgroundColor = `rgb(${colorTweenRGB(
    [20, 20, 50],
    [98, 144, 241],
    1 - t
  ).join(",")})`;
}

setInterval(setColor, 30);

function colorTweenRGB(a, b, t) {
  return a.map((av, i) => (b[i] - av) * t + av);
}

              
            
!
999px

Console