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.
<!--
Copyright (c) 2018 Ricardo Mendieta
Released under the MIT license
http://opensource.org/licenses/mit-license.php
-->
<div class="original">参考:<q><a href="https://codepen.io/mendieta/pen/WgvENJ">Ink Cursor</a></q></div>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="800">
<defs>
<filter id="goo">
<!-- ぼかし -->
<feGaussianBlur in="SourceGraphic" stdDeviation="6" result="blur" />
<!-- 透過度に対してコントラストをつける -->
<feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 35 -15" result="goo" />
<feComposite in="SourceGraphic" in2="goo" operator="atop" />
</filter>
</defs>
</svg>
<div id="cursor" class="Cursor"></div>
/*
Copyright (c) 2018 Ricardo Mendieta
Released under the MIT license
http://opensource.org/licenses/mit-license.php
*/
.Cursor {
pointer-events: none;
position: fixed;
display: block;
border-radius: 0;
transform-origin: center center;
top: 0;
left: 0;
z-index: 1000;
filter: url("#goo");
color: #000;
span {
position: absolute;
color: #000;
display: block;
width: 30px;
height: 30px;
border-radius: 50%;
transform-origin: center center;
transform: translate(-50%, -50%);
animation: bright-tone-rotation 8s infinite;
}
}
@keyframes bright-tone-rotation {
0% {
background-color: #f9344c;
}
8% {
background-color: #fc4e32;
}
17% {
background-color: #ff9914;
}
25% {
background-color: #fff231;
}
33% {
background-color: #99d02b;
}
42% {
background-color: #33a65e;
}
50% {
background-color: #1aa18e;
}
58% {
background-color: #1d86ae;
}
67% {
background-color: #386cb0;
}
75% {
background-color: #6964ad;
}
83% {
background-color: #a45aaa;
}
92% {
background-color: #df4c94;
}
100% {
background-color: #f9344c;
}
}
html,body {
height: 100vh;
width: 100vw;
background-color: #000;
overflow: hidden;
}
svg {
display: none;
}
.original {
font-size: 20px;
color: #fff;
a {
color: inherit;
}
}
/**
* Copyright (c) 2018 Ricardo Mendieta
* Released under the MIT license
* http://opensource.org/licenses/mit-license.php
*/
const cursor = document.getElementById('cursor')
const amount = 26 //生成する円の数
const sineDots = Math.floor(amount * 0.4) //蠢かせる円の数
const width = 30 //円の大きさ
const idleTimeout = 100 //カーソルが動いてる状態から静止してうごめく状態になるまでの時間[ms]
let mousePosition = { x: 0, y: 0}
let dots: Dot[] = []
let timeoutID: number
let idle = false
class Dot {
x = 0 // x座標
y = 0 // y座標
private anglespeed = 0.05 //動くスピード(可変)
private element = document.createElement('span')
private lockX = this.x // カーソルが止まった時のx座標
private lockY = this.y // カーソルが止まった時のy座標
private scale: number // 円の大きさ
private range: number // 円が動く範囲
private angleX = 0 // 0 ~ 2π の乱数:(後述)
private angleY = 0 // 0 ~ 2π の乱数:(後述)
constructor(
private index: number, // 円のindex(0 ~ amount)
){
// 円の大きさ(可変)
// indexが大きくなる = 後に生成された円ほど小さくなる
this.scale = 1 - 0.04 * this.index
// 円が動く範囲(可変)
this.range = width / 2 - width / 2 * this.scale + 2
this.element.classList.add(String(this.index))
gsap.set(this.element, { scale: this.scale });
cursor?.appendChild(this.element)
}
//カーソルが止まった時に1度だけ呼び出される
public lock() {
this.lockX = this.x;//とまったときのx座標の位置
this.lockY = this.y;//とまったときのy座標の位置
this.angleX = Math.PI * 2 * Math.random();// (0 ~ 2πの乱数)。カーソルが止まった時に乱数固定
this.angleY = Math.PI * 2 * Math.random();
}
//止まった円の描画
//メインのストーカー(最大幅の円)はカーソルの位置で固定
//それより小さいストーカーが蠢いている
public draw() {
//idle: うごめいているフラグ、 sinDotes: これ以下のインデックスはうごめかせない
if (!idle || this.index <= sineDots) {//うごめいていない(とまっていない) or インデックスがsinDots以下(蠢かせない円) === うごめかせる円もうごめかせない円も描写
gsap.set(this.element, {x: this.x, y: this.y})
} else {//うごめいている(とまっている) and インデックスがsinDots以上(うごめかせる円) == うごめかせるたまだけの描写
this.angleX += this.anglespeed;//うごめく速度
this.angleY += this.anglespeed;//うごめく速度
// this.lockX = 0
this.y = this.lockY + Math.sin(this.angleY) * this.range; //y方向のうごめき
this.x = this.lockX + Math.cos(this.angleX) * this.range; //x方向のうごめき
gsap.set(this.element, { x: this.x, y: this.y });
}
}
}
// 円の生成
const buildDots = () => {
for (let i = 0; i < amount; i++) {
let dot = new Dot(i)
dots.push(dot)
}
}
const onMouseMove = (e: MouseEvent) => {
mousePosition.x = e.clientX - width / 2
mousePosition.y = e.clientY - width / 2
resetIdleTimer()
}
const onTouchMove = (e: TouchEvent) => {
mousePosition.x = e.touches[0].clientX - width / 2
mousePosition.y = e.touches[0].clientY - width / 2
resetIdleTimer()
}
//カーソルが動いてる状態から静止してうごめく状態にする
const startIdleTimer = () => {
timeoutID = setTimeout(goInactive, idleTimeout);//idolTimeout[ms]後に蠢く状態にする
idle = false;//蠢いていない
}
const resetIdleTimer= () => {
clearTimeout(timeoutID);
startIdleTimer();
}
//うごめくきっかけの関数
//カーソルが動いている状態 => 静止してうごめく際に1回だけ呼ばれる
const goInactive = () => {
idle = true;//蠢いている
for (let dot of dots) {
dot.lock();
}
}
// 描画更新
const positionCursor = () => {
let x = mousePosition.x
let y = mousePosition.y
dots.forEach((dot, index, dots) => {
let nextDot = dots[index + 1] || dots[0]
const maxDistance = width / 1.6
dot.x = x
dot.y = y
dot.draw()
if (!idle || index <= sineDots) {
let dx = (nextDot.x - dot.x) * 0.4;
let dy = (nextDot.y - dot.y) * 0.4;
/** original */
if (dx >= maxDistance) {
dx = maxDistance
} else if (dx < -maxDistance) {
dx = -1 * maxDistance
}
if (dy >= maxDistance) {
dy = maxDistance
} else if (dy < -maxDistance) {
dy = -1 * maxDistance
}
/** ./original */
x += dx;
y += dy;
}
})
}
// レンダリング
const render = () => {
positionCursor()
requestAnimationFrame(render)
}
// 初期化
const init = () => {
window.addEventListener('mousemove', onMouseMove)
window.addEventListener('touchmove', onTouchMove)
buildDots()
render()
}
init()
Also see: Tab Triggers