JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
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.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button id="replay">replay</button>
<canvas id="canvas"></canvas>
</body>
</html>
canvas{
width:400px;
heigth:400px;
}
const cos = Math.cos,
acos = Math.acos,
min = Math.min,
max = Math.max,
tau = 2 * Math.PI,
crt = function (v) {
if (v < 0) return -Math.pow(-v, 1 / 3);
return Math.pow(v, 1 / 3);
},
sqrt = function (v) { return Math.sqrt(v); };
const canvas = document.getElementById("canvas")
const context = canvas.getContext("2d")
const canvasWidth = 1050
const canvasHeight = 1050
const cap = 50
const colNum = Math.ceil(canvasWidth / cap) - 1
const rowNum = Math.ceil(canvasHeight / cap) - 1
const initX = 50
const initY = 1000
const lineWidth = 4
let Grids = []
let cache = null
const points = [
[0, 0],
[75, 750],
[875, 360],
[1000, 1000]
].map((p) => [p[0] + initX, initY - p[1]])
let initPoint = points[0], x = 0, isEnded = false
const replay = document.getElementById("replay")
//
canvas.width = canvasWidth
canvas.height = canvasHeight
context.lineWidth = 10
context.fillStyle="white"
context.fillRect(0, 0, canvas.width, canvas.height)
//建立网格
function initGrid(cap, width, height, lineWidth) {
const colNum = Math.ceil(width / cap) - 1
const rowNum = Math.ceil(height / cap) - 1
for (let i = 1; i <= colNum; i++) {
Grids.push([[cap * i - lineWidth/4, 0, lineWidth/2, colNum * cap], [(i - 1) * cap, cap * i - lineWidth/2, colNum * cap + 5, "top", "center"]])
}
for (let i = 1; i <= rowNum; i++) {
Grids.push([[initX, cap * i - lineWidth/2, rowNum * cap, lineWidth/2], [(rowNum - i) * cap, initX - 5, cap * i - lineWidth/4, "middle", "right"]])
}
}
initGrid(cap, canvasWidth, canvasHeight, lineWidth);
function createGrid() {
context.fillStyle = '#999';
context.font = "24px Arial"
Grids.forEach((grid) => {
context.textAlign = grid[1][4]
context.textBaseline = grid[1][3]
context.fillRect(grid[0][0], grid[0][1], grid[0][2], grid[0][3])
context.save()
context.fillText(grid[1][0], grid[1][1], grid[1][2]);
context.restore()
})
}
createGrid()
replay.addEventListener("click", () => {
if (isEnded) {
initPoint = points[0]
isEnded = false;
x = 0;
context.fillStyle="white"
context.fillRect(0, 0, canvas.width, canvas.height)
createGrid()
animation2()
}
})
animation2()
function animation2() {
if (x > 1040) { isEnded = true; return }
let t = cardano(points, x)[0] || 0
//曲线
context.strokeStyle = "#d2fe9e"
let initPoint2 = drawCubicBezierCurzeByX(points, t)
drawLine(initPoint[0], initPoint[1],initPoint2[0], initPoint2[1])
//进度曲线
context.save()
context.strokeStyle = "#9eccfe"
drawLine(initX,initPoint[1],initX,initPoint2[1])
drawLine(initPoint[0],initY,initPoint2[0], initY)
initPoint=initPoint2
drawCubicBezierCurzeHelper(points, initPoint[2], [initPoint[0], initPoint[1]])
let img = new Image();
img.src = cache
img.onload = function () {
context.drawImage(img, 0, 0, canvas.width, canvas.height)
window.requestAnimationFrame(() => {
x += 10
animation2()
})
}
}
function drawCubicBezierCurzeHelper(points, t, circle) {
let [p0, p1, p2, p3] = points
let a = linearBezierCurze(...p0, ...p1)(t)
let b = linearBezierCurze(...p1, ...p2)(t)
let c = linearBezierCurze(...p2, ...p3)(t)
let e = linearBezierCurze(...a, ...b)(t)
let f = linearBezierCurze(...b, ...c)(t)
context.save()
context.strokeStyle = "gray"
points.reduce((p, c) => {
drawLine(...p,...c)
return c
})
cache = canvas.toDataURL("image/jpeg",1);
context.strokeStyle = "orange"
drawLine(...a,...b)
drawLine(...b,...c)
context.strokeStyle = "#f5147f"
drawLine(...e,...f)
context.restore()
}
function drawCubicBezierCurzeByX(points, x) {
let [p0, p1, p2, p3] = points,
[x0, y0] = p0,
[x1, y1] = p1,
[x2, y2] = p2,
[x3, y3] = p3
let a = -(x0 - 3 * x1 + 3 * x2 - x3),
b = 3 * (x0 - 2 * x1 + x2),
c = -3 * (x0 - x1),
d = x0 - x
tri = 18 * a * b * c * d - 4 * b * b * b * d + b * b * c * c - 4 * a * c * c * c - 27 * a * a * d * d
tri0 = b * b - 3 * a * c
tri1 = 2 * b * b * b - 9 * a * b * c + 27 * a * a * d
tmp = -27 * a * a * tri//==Math.pow(tri1,2)-4*Math.pow(tri0,3)
let C1 = (tri1 - Math.sqrt(tmp)) / 2, C2 = (tri1 + Math.sqrt(tmp)) / 2
C1 = C1 < 0 ? -Math.pow(-C1, 1 / 3) : Math.pow(C1, 1 / 3)
C2 = C2 < 0 ? -Math.pow(-C2, 1 / 3) : Math.pow(C2, 1 / 3)
let t=x
return [
Math.pow(1 - t, 3) * x0 + 3 * Math.pow(1 - t, 2) * t * x1 + 3 * t * t * (1 - t) * x2 + Math.pow(t, 3) * x3,
Math.pow(1 - t, 3) * y0 + 3 * Math.pow(1 - t, 2) * t * y1 + 3 * t * t * (1 - t) * y2 + Math.pow(t, 3) * y3,
t
]
}
function drawLine(x0,y0,x1,y1){
context.beginPath();
context.moveTo(x0, y0);
context.lineTo(x1, y1);
context.stroke()
context.closePath();
}
function linearBezierCurze(a, b, c, d) {//(a,b),(c,d)
let s1 = c - a, s2 = d - b
return function (t) {
return [
s1 * t + a,
s2 * t + b
]
}
}
function cardano(points, x) {
let [pt0, pt1, pt2, pt3] = points,
[pa, y0] = pt0,
[pb, y1] = pt1,
[pc, y2] = pt2,
[pd, y3] = pt3,
d = (-pa + 3 * pb - 3 * pc + pd),
a = (3 * pa - 6 * pb + 3 * pc) / d,
b = (-3 * pa + 3 * pb) / d,
c = (pa - x) / d,
p = (3 * b - a * a) / 3,
p3 = p / 3,
q = (2 * a * a * a - 9 * a * b + 27 * c) / 27,
q2 = q / 2,
discriminant = q2 * q2 + p3 * p3 * p3,
u1, v1, x1, x2, x3;
if (discriminant < 0) {
var mp3 = -p / 3,
mp33 = mp3 * mp3 * mp3,
r = sqrt(mp33),
t = -q / (2 * r),
cosphi = t < -1 ? -1 : t > 1 ? 1 : t,
phi = acos(cosphi),
crtr = crt(r),
t1 = 2 * crtr;
x1 = t1 * cos(phi / 3) - a / 3;
x2 = t1 * cos((phi + tau) / 3) - a / 3;
x3 = t1 * cos((phi + 2 * tau) / 3) - a / 3;
return [x1, x2, x3].filter((xx) => xx >= 0 && xx <= 1);
}
else if (discriminant === 0) {
u1 = q2 < 0 ? crt(-q2) : -crt(q2);
x1 = 2 * u1 - a / 3;
x2 = -u1 - a / 3;
return [x1, x2].filter((xx) => xx >= 0 && xx <= 1);
}else {
var sd = sqrt(discriminant),
tt = -q2 + sd;
u1 = crt(-q2 + sd);
v1 = crt(q2 + sd);
x1 = u1 - v1 - a / 3;
return [x1].filter((xx) => xx >= 0 && xx <= 1);
}
}
Also see: Tab Triggers