HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<body>
<div class="wrapper"></div>
<div class="indicator"></div>
<div class="sign">
by masahito / <a href="http://www.ma5a.com/" >ma5a.com</a>
</div>
</body>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
background-color: rgb(231, 251, 253);
font-family: sans-serif;
}
.wrapper {
position: absolute;
width: 100%;
height: 100%;
overflow: hidden;
cursor: pointer;
}
.penguin_wrapper {
position: absolute;
width: 96px;
height: 96px;
overflow: hidden;
transition: 0.5s;
}
svg {
display: block;
}
.penguin_wrapper svg {
fill: navy;
}
.penguin_inner_wrapper {
height: 100%;
width: 100%;
transition: 0;
}
.facing_upleft,
.facing_left,
.facing_downleft {
transform: scale(-1 , 1);
}
.penguin_sprite {
position: absolute;
height: calc(6 * 96px);
width: calc(8 * 96px);
pointer-events: none;
}
.penguin_sprite img {
height: 100%;
width: 100%;
display: block;
}
.hit_wrapper {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
pointer-events: none;
}
.hit_area {
height: 50px;
width: 40px;
display: flex;
flex-wrap: wrap;
/* border: red 1px solid; */
}
.sign {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
color: navy;
bottom: 10px;
right: 10px;
font-size: 10px;
}
a {
color: navy;
text-decoration: none;
}
a:hover { text-decoration: underline; }
.foot_print {
height: 30px;
width: 30px;
position: absolute;
z-index: -10;
image-rendering: pixelated;
background-size: 30px;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAAAXNSR0IArs4c6QAAAHlJREFUSEtjZBggwDhA9jKMWky3kB8N6oEP6tkv//0HuSJVnIkm0YHTULpbDLMQPcyp7XMMHw86i2EhQC2fE+1jmlsMs4DWQU4wVdMqkRHMo+g+p1kco/twwCymVRlKMKhHLaZWCIwGNbVCkqA5o0FNMIiopWDkBTUAjfAoH+hhPScAAAAASUVORK5CYII=);
animation: forwards fade 10s;
}
@keyframes fade {
0% {opacity: 0.4}
50% {opacity: 0.4}
100% {opacity: 0}
}
@keyframes fade_lighter {
0% {opacity: 0.1}
50% {opacity: 0.1}
100% {opacity: 0}
}
.jump_print {
height: 32px;
width: 36px;
position: absolute;
z-index: -10;
image-rendering: pixelated;
background-size: 36px 32px;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACQAAAAgCAYAAAB6kdqOAAAAAXNSR0IArs4c6QAAAK1JREFUWEftl8ENgCAMRWEKJ3QqJ3QKiAc81NTWT4AfU6+KvL4WCjmRPZmMJ8FAx1nKWzD7lqF/Q4MukOVAFoCVeq8xt6HlQL0AqDHVEA3QaBBpTtbWw1AAif2K19DsVGm1dBsKIKGorbYwpO3gYcjb23hrqEUwe/lHL/PWTvuOt5fJSEbXknbG5j8xajnvNfa/W4e1OixjXiNyHve97GvRTweyDKLvYUPohNa4Cm6ZcCHJb/u+AAAAAElFTkSuQmCC);
animation: forwards fade_lighter 10s;
}
.star{
position: absolute;
width: 16px;
height: 16px;
overflow: hidden;
z-index: 20;
}
.star_sprite {
position: absolute;
height: 100%;
width: calc(4 * 16px);
pointer-events: none;
}
.star svg {
fill: rgb(148, 234, 255)
}
.star.twinkle {
animation: infinite twinkle 1s;
}
@keyframes twinkle {
0% { transform: scale(1); }
50% { transform: scale(1.5); }
100% { transform: scale(1); }
}
function init() {
const decodeRef = { a: ' h 1', b: ' h 2', e: ' h 3', g: ' h 4', j: ' h 5', A: ' v 1', B: ' v 2', E: ' v 3', G: ' v 4', J: ' v 5', n: 'h -1', u: ' h -2', k: ' h -3', x: ' h -4', i: ' h -5', N: ' v -1', U: ' v -2', K: ' v -3', X: ' v -4', I: ' v -5', w: ' v ', W: ' h ', D: '<path d="M', o: '<path fill="pink" d="M', F: '<path fill="#fff" d="M', '/': '/>', d: '<path d="M', f: '<path fill="#fff" d="M'}
const decode = arr => arr.split('').map(c=> !decodeRef[c] ? c : decodeRef[c]).join('')
const penguinSvg = {
up: `d 22 11eAbBaBaEaBaAaAaAbAaExGnBuAaBnAkKnNnAuBxUaNuNnInAuAuKaNaNaNaNaNaIaUaUaNbN"/D 68 12jAaAaBaEaBbAbAaAbBiw6nExUnAxAaBkKuNnInAuAuKaUaNbNaUaIaUaNaN"/D 167 12eAbAaBaJaAaBbAaAaAaBW-6AnEnGkUaNuNuNuAaBxUaNuUnKiUaNbNbNaNaUaKaUaNbN"/D 115 13W6AaAaBaGaAbAbAeAaBnAxNnJnAnAkNnAkAaBnAuNnUaNkUnXnAxKaNbNaNaXaXaNaN"/D 215 13gAbBaGaEaAbAaAaGkNnEnBuGkXkNnAxNnNnKnAuAuKaNaNaNaNaNaNaKaKaNaNbN"/`,
dUp: `d 22 12eAbAaAaAaJaBbAaAaBaEaBiAnAnAnEkUnNxBuUkUnKkKaNaNaNaNaUaXaKaNbN"/D 69 12jAaAaEaBaBaAaAaAbAaAaExNnGnAnExNxAaBuAuKxUaKnNxUaNaNaUbUaXaKaUbN"/D 116 12jAaAeBnAaBaAaAbAbAbAaAaAnAxNuAaJnAnBuAuNiEkKkw-6nAxUaUbUbNaIaXaNaN"/D 167 13jAaAaAaw7aAaAaAaAaAaAaEiNuBnBuAaBxUnNnNuAkNuUnXaNiUaNeNaNaNaNaUaUaNaNaN"/D 216 13W6AaAaAaw8nAbAaBaAaGkNkAnBnEkUuNuNiNkw-6aUxUaNeNeNaNaNaUaNaNaN"/`,
side: `d 23 11eAeAaBaBeBnAkBaAaAbAaAaBaEkNnNnNnKnUnNnNnNkAuBnBnBnBnBnEnNnNnUnKaKaNaNaUaUaXaNaNbN"/D 69 12jAbAaAjBnAuAnEaAaAbAbAaEkNkUnKnNuNuAuAuBnAnw7aAaAbEkNnKuNnUaUnA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nNuAkNnAuAnBnGnEnAuAkXaNaUaNaNaNnIaKaNbN"/F 21 16aBnU"/F 122 16aBnU"/F 215 16aBnU"/F 75 17aBnU"/F 167 17aAnN"/F 68 18aBnU"/F 114 18aBnU"/F 173 18aAnN"/F 22 20gAaAaNaAaBaEaBaAnBnAnAkAnAxNuAnNuNnXaNaUaXaNaNaN"/F 118 20aAeNbAaAaAaBaJnBuAnAnAuAxNkNnw-6aXaUaNbN"/F 165 20jAaAaNaAaAaEaw6nAnAkAiNiUnXaUaUaUaNaN"/F 215 20gAaAbAaEaw6nAnAn3XaXaUaNaNaNaN"/F 69 21gAbNaAaAaBaAaJnAuAnBuAxNnAnNuUnKaUaIaNaN"/D 77 32aAaBkUaN"/D 125 32eEkNnNaN"/D 160 32aAnN"/D 27 34eAaBxK"/D 163 34eAaBkNnU"/D 171 34eAaBkNnU"/D 211 34gBxU"/D 219 34bAbBnAkX"/D 20 35bAaBnAuX"/D 69 35aAbBuAuKaN"/D 116 35aAbAaAnAkX"/`,
down: `d 22 11eAbBaBaEaBaAaAaAbAaEiUnUnUnNnNuAnAnNnNuAnBnBnBnAnAuAuXaNaNaNaNaNaXaUaUaNbN"/D 68 12jAaAaBaEaAaAaAbAaAbBiNnUnUnNuNnAnBuUuAnAnBnEnAnAuAuKaUaNaNaNaNaIaUaNaN"/D 167 12eAbAaBaJaAaBaAbAaAaBiNnNnKnKuNnAnAnNnNuAuBnBnBuAkNnNaNaNbNaNaNaUaKaUaNbN"/D 115 13W6AaAaBaEaBbAbAbAaAaAnAxNuNnUnNnNnNkBnNnNnAuBnBnBnAxKaNaNaNaNaXaKaNaN"/D 215 13gAbBaGaEaAbAaAaGxNnNnKnUnNnNnAnAnNnNuAnAnBnBnAnAxUaUaNbNaNaKaKaNaNbN"/F 19 16aBnU"/F 26 16aBnU"/F 73 16aBnU"/F 165 16aBnU"/F 67 17aBnU"/F 120 17aAnN"/F 171 17aBnU"/F 213 17aBnU"/F 114 18aAnN"/F 219 18aBnU"/F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aBnU"/F 172 18aBnU"/F 119 19aBnU"/F 126 19aBnU"/F 307 19eAaAaNaNbBaAaBaBaBnAnAiNuAW-6NnUaUaKaUaN"/D 260 20W6AaAaAaAaBbAgBnAnAuNuUnNuNkAuNxAnAnBuAuNuNnUgNeUaUaNbN"/F 213 21aBnU"/F 219 21aBnU"/F 75 22gBaw7nBnAW-8IaNaUaUaNaN"/F 121 22bAaAaUaAaEaw6nAnAxNnNkNnUnNaKaNaNbN"/F 167 22aAaAaNaNaAaAaBaBaAaEnAnAiNkAxNnNnXaNaNaUaNbN"/F 259 24bAuN"/F 265 24bAuN"/D 80 25aAaBaBaAnAnNnNnI"/F 213 25bAaAaNaNeAaBaAaAaGnAn3NuNnKaNaNaNaNaN"/F 258 26gAbNeAbAaBaAaEnAnAn4NnNnKaNaUaNaN"/D 353 29bBnAkUbN"/D 364 29bAbBkNnU"/D 306 30bBnAuUaN"/D 314 30bAaBuNnU"/D 75 34bAaBkK"/D 124 34bAaBuNnU"/D 164 34bEkUaN"/D 171 34bAaBkK"/D 211 35eBxNaN"/D 219 35eAaAxU"/D 258 36eAkN"/D 266 36eAkN"/`
}
const starSvg = `d 7 0bBaBaAjAeUaNaEbAaAkBnAnKuNuAnAnBaBaBkNnNuNuAuAnAkUaUaUnNnNnUjNaUaU"/D 27 1aEbAaAkBnAnKuNnNeUaN"/D 44 1aBaAaAuAnAnUnNnNbNaN"/D 60 1aBbAuBnUuNbU"/D 35 4aBaAaAuAnAnUnNnNbNaN"/D 50 4aBbAuBnUuNbU"/D 24 8aEbAaAkBnAnKuNnNeUaN"/D 41 10aBaAaAuAnAnUnNnNbNaN"/D 57 11aBbAuBnUuNbU"/`
const body = document.querySelector('.wrapper')
const animationFrames = {
walk: [0, 1, 2, 1, 3, 4],
stop: [0],
celebrate: [5, 6, 7, 6, 3, 4],
turnFromup: [0, 1, 2, 3, 4],
turnFromdUp: [1, 2, 3, 4],
turnFromside: [2, 3, 4],
turnFromdDown: [3, 4],
turnFromdown: [4],
}
const cellSize = 96
const directions = {
up: 0,
dUp: 1,
side: 2,
dDown: 3,
down: 4,
turn: 5,
}
const spriteNo = Object.keys(penguinSvg).length
const sprites = {
up: 'up',
upright: 'dUp',
right: 'side',
downright: 'dDown',
down: 'down',
downleft: 'dDown',
left: 'side',
upleft: 'dUp',
turn: 'turn'
}
const turnDirections = Object.keys(sprites)
const frameSpeed = 100
const control = {
x: null,
y: null,
angle: 360,
}
const directionConversions = {
360: 'up',
45: 'upright',
90: 'right',
135: 'downright',
180: 'down',
225: 'downleft',
270: 'left',
315: 'upleft',
}
const distance = 10
let star
let starInterval
const setPos = (target, x, y) =>{
target.style.left = `${x}px`
target.style.top = `${y}px`
}
const setMargin = (target, x, y) =>{
target.style.transform = `translate(${x}px, ${y}px)`
}
const reverseDirectionConversions = () =>{
const obj = {}
Object.keys(directionConversions).forEach( k =>{
obj[directionConversions[k]] = k
})
return obj
}
const animatePenguin = penguin =>{
const { frame:i, animation, frameSpeed } = penguinData
const penguinSprite = penguin.childNodes[1].childNodes[1]
setMargin(penguinSprite, `-${animationFrames[animation][i] * cellSize}`, `-${cellSize * directions[sprites[penguinData.direction]]}`)
penguinData.frame = i === animationFrames[animation].length - 1 ? 0 : i + 1
penguinData.frameTimer = setTimeout(()=> animatePenguin(penguin), frameSpeed)
if (animation === 'celebrate' && i === 0) {
penguinData.jump
? createJumpMark(penguin)
: penguinData.jump = true
}
if (animation !== 'celebrate') penguinData.jump = false
}
const randomDirection = () =>{
return turnDirections.filter(t => t !== 'turn')[Math.floor(Math.random() * turnDirections.length - 1)]
}
const penguinData = {
penguin: null,
animation: 'walk',
frame: 0,
direction: randomDirection(),
frameTimer: null,
moveTimer: null,
turnIndex: Math.floor(Math.random() * 7),
frameSpeed,
moveSpeed: 150,
stop: true,
stepLeft: true,
marginPos: { x: 0, y: 0 },
pos: { x: 0, y: 0 }
}
const radToDeg = rad => Math.round(rad * (180 / Math.PI))
const nearestN = (n, denom) =>{
return n === 0 ? 0 : (n - 1) + Math.abs(((n - 1) % denom) - denom)
}
const overlap = (a, b) =>{
const buffer = 20
return Math.abs(a - b) < buffer
}
const clickedAngle = () =>{
const angle = radToDeg(Math.atan2(penguinData.pos.y - control.y, penguinData.pos.x - control.x)) - 90
const adjustedAngle = angle < 0 ? angle + 360 : angle
return nearestN(adjustedAngle, 45)
}
const animateSvg = ({ target, start, end, interval, speed }) => {
const startFrame = start || 0
let i = startFrame
clearInterval(interval)
interval = setInterval(()=> {
target.style.marginLeft = `${-(i * 100)}%`
i = i >= end
? startFrame
: i + 1
}, speed || 100)
}
const changeAnimation = animation => {
penguinData.frame = 0
penguinData.animation = animation
}
const stopPenguin = animation =>{
changeAnimation(animation)
penguinData.stop = true
}
const startPenguin = penguin =>{
animatePenguin(penguin)
penguinData.stop = false
}
const checkBoundaryAndUpdatePenguinPos = (x, y, penguin, penguinData) =>{
const lowerLimit = -40 // buffer from window edge
const upperLimit = 40
if (x > lowerLimit && x < (body.clientWidth - upperLimit)){
penguinData.marginPos.x = x
}
if (y > lowerLimit && y < (body.clientHeight - upperLimit)){
penguinData.marginPos.y = y
}
setMargin(penguin, x, y)
}
const createMark = (penguin, angle) => {
const { height, left, top } = penguin.getBoundingClientRect()
const mark = document.createElement('div')
penguinData.stepLeft = !penguinData.stepLeft
mark.className = 'foot_print'
mark.style.transform = `rotate(${angle}deg) scale(${penguinData.stepLeft ? -1 : 1}, 1)`
penguinData.pos.x = left + 5
penguinData.pos.y = top + (height - 20)
setPos(mark, penguinData.pos.x, penguinData.pos.y)
body.append(mark)
setTimeout(()=> body.removeChild(mark), 10000)
}
const createJumpMark = penguin =>{
const { left, top } = penguin.childNodes[1].childNodes[3].childNodes[1].getBoundingClientRect()
const mark = document.createElement('div')
mark.className = 'jump_print'
setPos(mark, left + 1, top + 33)
body.append(mark)
setTimeout(()=> body.removeChild(mark), 10000)
}
const touchedTheStar = (x, y) =>{
return x === penguinData.marginPos.x && y === penguinData.marginPos.y ||
overlap(control.x, penguinData.pos.x) && overlap(control.y, penguinData.pos.y)
}
const moveAbout = penguin =>{
const penguinDir = +reverseDirectionConversions()[penguinData.direction]
const angle = clickedAngle()
const turnValue = penguinDir === angle
? 0
: penguinDir < angle
? 1
: -1
penguinData.turnIndex += turnValue
if (penguinData.turnIndex < 0) penguinData.turnIndex = 7
if (penguinData.turnIndex > 7) penguinData.turnIndex = 0
penguinData.direction = turnDirections[penguinData.turnIndex]
const { direction: dir } = penguinData
penguin.childNodes[1].className = `penguin_inner_wrapper facing_${dir}`
createMark(penguin.childNodes[1].childNodes[3].childNodes[1], penguinDir)
let { x, y } = penguinData.marginPos
if (dir !== 'up' && dir !== 'down') x += (dir.includes('left')) ? -distance : distance
if (dir !== 'left' && dir !== 'right') y += (dir.includes('up')) ? -distance : distance
if (touchedTheStar(x, y)){
checkBoundaryAndUpdatePenguinPos(control.x, control.y, penguin, penguinData)
stopPenguin('turnFrom' + sprites[dir])
penguinData.direction = 'turn'
setTimeout(()=>{
if (penguinData.direction === 'turn') {
stopPenguin('celebrate')
}
}, 100 * animationFrames['turnFrom' + sprites[dir]].length)
star.classList.remove('twinkle')
animateSvg({
target: star.childNodes[1],
end: 3,
interval: starInterval,
})
setTimeout(()=>{
if (touchedTheStar(x, y)) {
clearInterval(starInterval)
body.removeChild(star)
star = null
}
}, 400)
}
checkBoundaryAndUpdatePenguinPos(x, y, penguin, penguinData)
if (!penguinData.stop) penguinData.moveTimer = setTimeout(()=> {
moveAbout(penguin)
}, penguinData.moveSpeed)
}
const svgWrapper = ({ content, w, h, frameWidth, spriteNo }) => `<svg x="0px" y="0px" width="${frameWidth}%" height="${100 / spriteNo}%" viewBox="0 0 ${w} ${h}">${content}</svg>`
const mapPenguinAssets = () => Object.keys(directions).map(dir => {
return dir === 'turn'
? svgWrapper({
content: decode(penguinSvg[dir]),
w: 384, h: 48,
frameWidth: 100,
spriteNo
})
: svgWrapper({
content: decode(penguinSvg[dir]),
w: 240, h: 48,
frameWidth: (100 / 8) * 5,
spriteNo
})
}).join('')
const createPenguin = (x, y) =>{
const penguin = document.createElement('div')
penguin.className = 'penguin_wrapper'
penguin.innerHTML = `
<div class="penguin_inner_wrapper">
<div class="penguin_sprite">
${mapPenguinAssets()}
</div>
<div class="hit_wrapper">
<div class="hit_area">
</div>
</div>
</div>
`
setMargin(penguin, x, y)
body.append(penguin)
const { width, height, left, top } = penguin.getBoundingClientRect()
Object.assign(penguinData, {
penguin: penguin,
marginPos: { x, y },
pos: {
x: left + (width / 2),
y: top + (height / 2),
}
})
startPenguin(penguin)
stopPenguin('stop')
}
createPenguin((body.clientWidth / 2) - 48, (body.clientHeight / 2) - 48)
const createStar = () =>{
if (star) {
body.removeChild(star)
star = null
}
star = document.createElement('div')
star.className = ('star twinkle')
star.innerHTML = `
<div class="star_sprite">
${svgWrapper({
content: decode(starSvg),
w: 64, h: 16,
frameWidth: 100, spriteNo: 1
})}
</div>`
setPos(star, control.x - 8, control.y - 8)
body.append(star)
}
body.addEventListener('click', e =>{
control.x = e.pageX
control.y = e.pageY
createStar()
if (penguinData.stop){
changeAnimation('walk')
penguinData.stop = false
moveAbout(penguinData.penguin, penguinData)
}
})
}
window.addEventListener('DOMContentLoaded', init)
Also see: Tab Triggers