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.
<svg id="input" viewBox="0 0 100 20">
<path d="M0,10 L100,10" />
</svg>
<svg id="_2" viewBox="0 0 100 20">
<path id="label" transform="translate(37,10) scale(.75)" d="M0.89,-1.79 Q0,0 -0.89,-1.79L-1.10,-2.21 Q-2,-4 -4,-4L-5,-4 Q-7,-4 -7,-6L-7,-10 Q-7,-12 -5,-12L5,-12 Q7,-12 7,-10L7,-6 Q7,-4 5,-4L4,-4 Q2,-4 1.1,-2.21Z" />
<circle cy="10" r="0.1" />
<text text-anchor="middle" transform="translate(0,1.6)" >
<textPath startOffset="53.5%" dominant-baseline="hanging" xlink:href="#label">
37
</textPath>
</svg>
<input type="range" id="witness" value="37" disabled>
svg {
width: 80%;
min-width:30em;
top:0;
bottom:0;
left:0;
right:0;
display: block;
margin: auto;
position: absolute;
cursor:pointer;
}
svg#input{border: 1px dotted #efefef;}
path {
stroke: #999;
stroke-width: 0.25px;
fill: none;
}
circle,
path,#_2 {
pointer-events: none;
}
#_2{overflow:visible;}
#_2 path{fill:black;stroke:none;}
text {
user-select: none;
fill: #fff;
font-size: 3px;
font-family:verdana;
}
input{display:block; margin:1em auto;}
const SVG_NS = "http://www.w3.org/2000/svg";
const SVG_XLINK = "http://www.w3.org/1999/xlink";
let svgH = 20;
let svg = document.querySelector("svg");
let rid = null;
deformation = 0;
let dragging = false;
let m = { x: 37, y: svgH / 2 };
class Slider {
constructor(svg) {
this.deformation = 10;
this.target = 5;
this.value = 0;
this.dragging = false;
this.svg = svg;
this.path = svg.querySelector("path");
this.thumb = _2.querySelector("circle");
this.label = label; ////
this.text = _2.querySelector("#_2 text textPath");
this.inputElement = witness; ////
this.svg.addEventListener("mousedown", e => {
this.dragging = true;
m = oMousePosSVG(e);
console.clear();
console.log(Math.round(m.x * 100) / 100);
this.target = 5;
Frame();
});
this.svg.addEventListener("mouseup", () => {
this.dragging = false;
this.target = 0;
Frame();
//this.updatePath(m,deformation);
});
this.svg.addEventListener("mouseout", () => {
this.dragging = false;
this.target = 0;
Frame();
});
this.svg.addEventListener("mousemove", e => {
if (this.dragging) {
m = oMousePosSVG(e);
console.clear();
console.log(Math.round(m.x * 100) / 100);
this.target = 5;
Frame();
}
});
}
updateValue() {
let dist = this.target - this.value;
let vel = dist / 10;
this.value += vel;
//improvement
if (Math.abs(dist) < 0.01) {
if (rid) {
window.cancelAnimationFrame(rid);
rid = null;
}
}
}
updatePath(m) {
this.d = curvedPath(m.x, svgH / 2, this.deformation, this.value);
this.path.setAttributeNS(null, "d", this.d);
this.thumb.setAttributeNS(null, "r", 0.1 + this.value / 3);
this.thumb.setAttributeNS(null, "cx", m.x);
this.updateLabel(m);
this.updateInput(m);
}
updateLabel(m) {
this.label.setAttributeNS(
null,
"transform",
`translate(${m.x},${svgH / 2 - this.value}) scale(.75)`
);
this.text.textContent = Math.round(m.x);
}
updateInput(m) {
this.inputElement.value = Math.round(m.x);
}
}
function oMousePosSVG(e) {
var p = svg.createSVGPoint();
p.x = e.clientX;
p.y = e.clientY;
var ctm = svg.getScreenCTM().inverse();
var p = p.matrixTransform(ctm);
return p;
}
let slider = new Slider(svg);
function Frame() {
rid = window.requestAnimationFrame(Frame);
slider.updateValue();
slider.updatePath(m);
}
// HELPERS
function curvedPath(X, Y, defX, defY) {
//let def = 5;//deformation
//let Y = 20;
//let X = mouse position
let D = { cx: X, cy: Y - defY, r: 1 };
let B = { cx: D.cx - defX, cy: Y, r: 1 };
let F = { cx: D.cx + defX, cy: Y, r: 1 };
let A = { cx: D.cx - 2 * defX, cy: Y, r: 1 };
let G = { cx: D.cx + 2 * defX, cy: Y, r: 1 };
let C = interpolatePoint(B, D, 1, 2);
C.r = 1;
let E = interpolatePoint(D, F, 1, 2);
E.r = 1;
return `M0,${Y} L${A.cx},${A.cy}
Q${B.cx},${B.cy} ${C.cx},${C.cy}
Q${D.cx},${D.cy} ${E.cx},${E.cy}
Q${F.cx},${F.cy} ${G.cx},${G.cy}
L100,${A.cy}
`;
}
function interpolatePoint(a, b, i, n) {
//point a
//point b
//line divided in n segments
//find the i-th point
var o = {
cx: a.cx + (b.cx - a.cx) * (i / n),
cy: a.cy + (b.cy - a.cy) * (i / n)
};
return o;
}
Also see: Tab Triggers