rough-notation
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -14,2 +14,3 @@ export declare const SVG_NS = "http://www.w3.org/2000/svg"; | ||
type: RoughAnnotationType; | ||
multiline?: boolean; | ||
} | ||
@@ -19,3 +20,2 @@ export interface RoughAnnotationConfigBase { | ||
animationDuration?: number; | ||
animationDelay?: number; | ||
color?: string; | ||
@@ -22,0 +22,0 @@ strokeWidth?: number; |
import { SVG_NS, DEFAULT_ANIMATION_DURATION } from './model.js'; | ||
import { line, rectangle, ellipse } from 'roughjs/bin/renderer'; | ||
const defaultOptions = { | ||
maxRandomnessOffset: 2, | ||
roughness: 1.5, | ||
bowing: 1, | ||
stroke: '#000', | ||
strokeWidth: 1.5, | ||
curveTightness: 0, | ||
curveFitting: 0.95, | ||
curveStepCount: 9, | ||
fillStyle: 'hachure', | ||
fillWeight: -1, | ||
hachureAngle: -41, | ||
hachureGap: -1, | ||
dashOffset: -1, | ||
dashGap: -1, | ||
zigzagOffset: -1, | ||
seed: 0, | ||
combineNestedSvgPaths: false, | ||
disableMultiStroke: false, | ||
disableMultiStrokeFill: false | ||
}; | ||
function getOptions(type, seed) { | ||
const o = JSON.parse(JSON.stringify(defaultOptions)); | ||
switch (type) { | ||
case 'highlight': | ||
o.roughness = 3; | ||
o.disableMultiStroke = true; | ||
break; | ||
case 'single': | ||
o.disableMultiStroke = true; | ||
break; | ||
} | ||
o.seed = seed; | ||
return o; | ||
return { | ||
maxRandomnessOffset: 2, | ||
roughness: type === 'highlight' ? 3 : 1.5, | ||
bowing: 1, | ||
stroke: '#000', | ||
strokeWidth: 1.5, | ||
curveTightness: 0, | ||
curveFitting: 0.95, | ||
curveStepCount: 9, | ||
fillStyle: 'hachure', | ||
fillWeight: -1, | ||
hachureAngle: -41, | ||
hachureGap: -1, | ||
dashOffset: -1, | ||
dashGap: -1, | ||
zigzagOffset: -1, | ||
combineNestedSvgPaths: false, | ||
disableMultiStroke: type !== 'double', | ||
disableMultiStrokeFill: false, | ||
seed | ||
}; | ||
} | ||
@@ -170,9 +158,9 @@ function parsePadding(config) { | ||
const totalDuration = config.animationDuration === 0 ? 0 : (config.animationDuration || DEFAULT_ANIMATION_DURATION); | ||
const initialDelay = (config.animationDelay === 0 ? 0 : (config.animationDelay || 0)) + (animationGroupDelay || 0); | ||
const setAttr = (p, an, av) => p.setAttribute(an, av); | ||
for (const d of pathStrings) { | ||
const path = document.createElementNS(SVG_NS, 'path'); | ||
path.setAttribute('d', d); | ||
path.setAttribute('fill', 'none'); | ||
path.setAttribute('stroke', config.color || 'currentColor'); | ||
path.setAttribute('stroke-width', `${strokeWidth}`); | ||
setAttr(path, 'd', d); | ||
setAttr(path, 'fill', 'none'); | ||
setAttr(path, 'stroke', config.color || 'currentColor'); | ||
setAttr(path, 'stroke-width', `${strokeWidth}`); | ||
if (animate) { | ||
@@ -192,3 +180,3 @@ const length = path.getTotalLength(); | ||
const duration = totalLength ? (totalDuration * (length / totalLength)) : 0; | ||
const delay = initialDelay + durationOffset; | ||
const delay = animationGroupDelay + durationOffset; | ||
const style = path.style; | ||
@@ -195,0 +183,0 @@ style.strokeDashoffset = `${length}`; |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const t="http://www.w3.org/2000/svg";class e{constructor(t){this.seed=t}next(){return this.seed?(2**31-1&(this.seed=Math.imul(48271,this.seed)))/2**31:Math.random()}}function s(t,e,s,i,n){return{type:"path",ops:c(t,e,s,i,n)}}function i(t,e){return function(t,e,i){const n=(t||[]).length;if(n>2){const s=[];for(let e=0;e<n-1;e++)s.push(...c(t[e][0],t[e][1],t[e+1][0],t[e+1][1],i));return e&&s.push(...c(t[n-1][0],t[n-1][1],t[0][0],t[0][1],i)),{type:"path",ops:s}}return 2===n?s(t[0][0],t[0][1],t[1][0],t[1][1],i):{type:"path",ops:[]}}(t,!0,e)}function n(t,e,s,n,o){return i([[t,e],[t+s,e],[t+s,e+n],[t,e+n]],o)}function o(t,e,s,i,n){return function(t,e,s,i){const[n,o]=f(i.increment,t,e,i.rx,i.ry,1,i.increment*h(.1,h(.4,1,s),s),s);let r=l(n,null,s);if(!s.disableMultiStroke){const[n]=f(i.increment,t,e,i.rx,i.ry,1.5,0,s),o=l(n,null,s);r=r.concat(o)}return{estimatedPoints:o,opset:{type:"path",ops:r}}}(t,e,n,function(t,e,s){const i=Math.sqrt(2*Math.PI*Math.sqrt((Math.pow(t/2,2)+Math.pow(e/2,2))/2)),n=Math.max(s.curveStepCount,s.curveStepCount/Math.sqrt(200)*i),o=2*Math.PI/n;let r=Math.abs(t/2),h=Math.abs(e/2);const c=1-s.curveFitting;return r+=a(r*c,s),h+=a(h*c,s),{increment:o,rx:r,ry:h}}(s,i,n)).opset}function r(t){return t.randomizer||(t.randomizer=new e(t.seed||0)),t.randomizer.next()}function h(t,e,s,i=1){return s.roughness*i*(r(s)*(e-t)+t)}function a(t,e,s=1){return h(-t,t,e,s)}function c(t,e,s,i,n,o=!1){const r=o?n.disableMultiStrokeFill:n.disableMultiStroke,h=u(t,e,s,i,n,!0,!1);if(r)return h;const a=u(t,e,s,i,n,!0,!0);return h.concat(a)}function u(t,e,s,i,n,o,h){const c=Math.pow(t-s,2)+Math.pow(e-i,2),u=Math.sqrt(c);let l=1;l=u<200?1:u>500?.4:-.0016668*u+1.233334;let f=n.maxRandomnessOffset||0;f*f*100>c&&(f=u/10);const g=f/2,d=.2+.2*r(n);let p=n.bowing*n.maxRandomnessOffset*(i-e)/200,_=n.bowing*n.maxRandomnessOffset*(t-s)/200;p=a(p,n,l),_=a(_,n,l);const m=[],w=()=>a(g,n,l),v=()=>a(f,n,l);return o&&(h?m.push({op:"move",data:[t+w(),e+w()]}):m.push({op:"move",data:[t+a(f,n,l),e+a(f,n,l)]})),h?m.push({op:"bcurveTo",data:[p+t+(s-t)*d+w(),_+e+(i-e)*d+w(),p+t+2*(s-t)*d+w(),_+e+2*(i-e)*d+w(),s+w(),i+w()]}):m.push({op:"bcurveTo",data:[p+t+(s-t)*d+v(),_+e+(i-e)*d+v(),p+t+2*(s-t)*d+v(),_+e+2*(i-e)*d+v(),s+v(),i+v()]}),m}function l(t,e,s){const i=t.length,n=[];if(i>3){const o=[],r=1-s.curveTightness;n.push({op:"move",data:[t[1][0],t[1][1]]});for(let e=1;e+2<i;e++){const s=t[e];o[0]=[s[0],s[1]],o[1]=[s[0]+(r*t[e+1][0]-r*t[e-1][0])/6,s[1]+(r*t[e+1][1]-r*t[e-1][1])/6],o[2]=[t[e+1][0]+(r*t[e][0]-r*t[e+2][0])/6,t[e+1][1]+(r*t[e][1]-r*t[e+2][1])/6],o[3]=[t[e+1][0],t[e+1][1]],n.push({op:"bcurveTo",data:[o[1][0],o[1][1],o[2][0],o[2][1],o[3][0],o[3][1]]})}if(e&&2===e.length){const t=s.maxRandomnessOffset;n.push({op:"lineTo",data:[e[0]+a(t,s),e[1]+a(t,s)]})}}else 3===i?(n.push({op:"move",data:[t[1][0],t[1][1]]}),n.push({op:"bcurveTo",data:[t[1][0],t[1][1],t[2][0],t[2][1],t[2][0],t[2][1]]})):2===i&&n.push(...c(t[0][0],t[0][1],t[1][0],t[1][1],s));return n}function f(t,e,s,i,n,o,r,h){const c=[],u=[],l=a(.5,h)-Math.PI/2;u.push([a(o,h)+e+.9*i*Math.cos(l-t),a(o,h)+s+.9*n*Math.sin(l-t)]);for(let r=l;r<2*Math.PI+l-.01;r+=t){const t=[a(o,h)+e+i*Math.cos(r),a(o,h)+s+n*Math.sin(r)];c.push(t),u.push(t)}return u.push([a(o,h)+e+i*Math.cos(l+2*Math.PI+.5*r),a(o,h)+s+n*Math.sin(l+2*Math.PI+.5*r)]),u.push([a(o,h)+e+.98*i*Math.cos(l+r),a(o,h)+s+.98*n*Math.sin(l+r)]),u.push([a(o,h)+e+.9*i*Math.cos(l+.5*r),a(o,h)+s+.9*n*Math.sin(l+.5*r)]),[u,c]}const g={maxRandomnessOffset:2,roughness:1.5,bowing:1,stroke:"#000",strokeWidth:1.5,curveTightness:0,curveFitting:.95,curveStepCount:9,fillStyle:"hachure",fillWeight:-1,hachureAngle:-41,hachureGap:-1,dashOffset:-1,dashGap:-1,zigzagOffset:-1,seed:0,combineNestedSvgPaths:!1,disableMultiStroke:!1,disableMultiStrokeFill:!1};function d(t,e){const s=JSON.parse(JSON.stringify(g));switch(t){case"highlight":s.roughness=3,s.disableMultiStroke=!0;break;case"single":s.disableMultiStroke=!0}return s.seed=e,s}function p(e,i,r,h,a){const c=[];let u=r.strokeWidth||2;const l=function(t){const e=t.padding;if(e||0===e){if("number"==typeof e)return[e,e,e,e];if(Array.isArray(e)){const t=e;if(t.length)switch(t.length){case 4:return[...t];case 1:return[t[0],t[0],t[0],t[0]];case 2:return[...t,...t];case 3:return[...t,t[1]];default:return[t[0],t[1],t[2],t[3]]}}}return[5,5,5,5]}(r),f=void 0===r.animate||!!r.animate,g=r.iterations||2;switch(r.type){case"underline":{const t=d("single",a),e=i.y+i.h+l[2];for(let n=0;n<g;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}case"strike-through":{const t=d("single",a),e=i.y+i.h/2;for(let n=0;n<g;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}case"box":{const t=d("single",a),e=i.x-l[3],s=i.y-l[0],o=i.w+(l[1]+l[3]),r=i.h+(l[0]+l[2]);for(let i=0;i<g;i++)c.push(n(e,s,o,r,t));break}case"crossed-off":{const t=d("single",a),e=i.x,n=i.y,o=e+i.w,r=n+i.h;for(let i=0;i<g;i++)i%2?c.push(s(o,r,e,n,t)):c.push(s(e,n,o,r,t));for(let i=0;i<g;i++)i%2?c.push(s(e,r,o,n,t)):c.push(s(o,n,e,r,t));break}case"circle":{const t=d("single",a),e=d("double",a),s=i.w+(l[1]+l[3]),n=i.h+(l[0]+l[2]),r=i.x-l[3]+s/2,h=i.y-l[0]+n/2,u=Math.floor(g/2),f=g-2*u;for(let t=0;t<u;t++)c.push(o(r,h,s,n,e));for(let e=0;e<f;e++)c.push(o(r,h,s,n,t));break}case"highlight":{const t=d("highlight",a);u=.95*i.h;const e=i.y+i.h/2;for(let n=0;n<g;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}}if(c.length){const s=function(t){const e=[];for(const s of t){let t="";for(const i of s.ops){const s=i.data;switch(i.op){case"move":t.trim()&&e.push(t.trim()),t=`M${s[0]} ${s[1]} `;break;case"bcurveTo":t+=`C${s[0]} ${s[1]}, ${s[2]} ${s[3]}, ${s[4]} ${s[5]} `;break;case"lineTo":t+=`L${s[0]} ${s[1]} `}}t.trim()&&e.push(t.trim())}return e}(c),i=[],n=[];let o=0;const a=0===r.animationDuration?0:r.animationDuration||800,l=(0===r.animationDelay?0:r.animationDelay||0)+(h||0);for(const h of s){const s=document.createElementNS(t,"path");if(s.setAttribute("d",h),s.setAttribute("fill","none"),s.setAttribute("stroke",r.color||"currentColor"),s.setAttribute("stroke-width",""+u),f){const t=s.getTotalLength();i.push(t),o+=t}e.appendChild(s),n.push(s)}if(f){let t=0;for(let e=0;e<n.length;e++){const s=n[e],r=i[e],h=o?a*(r/o):0,c=l+t,u=s.style;u.strokeDashoffset=""+r,u.strokeDasharray=""+r,u.animation=`rough-notation-dash ${h}ms ease-out ${c}ms forwards`,t+=h}}}}class _{constructor(t,e){this._state="unattached",this._resizing=!1,this._seed=Math.floor(Math.random()*2**31),this._animationGroupDelay=0,this._resizeListener=()=>{this._resizing||(this._resizing=!0,setTimeout(()=>{if(this._resizing=!1,"showing"===this._state){const t=this.size();t&&this.hasRectChanged(t)&&this.show()}},400))},this._e=t,this._config=JSON.parse(JSON.stringify(e)),this.attach()}get animate(){return this._config.animate}set animate(t){this._config.animate=t}get animationDuration(){return this._config.animationDuration}set animationDuration(t){this._config.animationDuration=t}get animationDelay(){return this._config.animationDelay}set animationDelay(t){this._config.animationDelay=t}get iterations(){return this._config.iterations}set iterations(t){this._config.iterations=t}get color(){return this._config.color}set color(t){this._config.color!==t&&(this._config.color=t,this.refresh())}get strokeWidth(){return this._config.strokeWidth}set strokeWidth(t){this._config.strokeWidth!==t&&(this._config.strokeWidth=t,this.refresh())}get padding(){return this._config.padding}set padding(t){this._config.padding!==t&&(this._config.padding=t,this.refresh())}attach(){if("unattached"===this._state&&this._e.parentElement){!function(){if(!window.__rno_kf_s){const t=window.__rno_kf_s=document.createElement("style");t.textContent="@keyframes rough-notation-dash { to { stroke-dashoffset: 0; } }",document.head.appendChild(t)}}();const e=this._svg=document.createElementNS(t,"svg");e.setAttribute("class","rough-annotation");const s=e.style;s.position="absolute",s.top="0",s.left="0",s.overflow="visible",s.pointerEvents="none",s.width="100px",s.height="100px";const i="highlight"===this._config.type;if(this._e.insertAdjacentElement(i?"beforebegin":"afterend",e),this._state="not-showing",i){const t=window.getComputedStyle(this._e).position;(!t||"static"===t)&&(this._e.style.position="relative")}this.attachListeners()}}detachListeners(){window.removeEventListener("resize",this._resizeListener),this._ro&&this._ro.unobserve(this._e)}attachListeners(){this.detachListeners(),window.addEventListener("resize",this._resizeListener,{passive:!0}),!this._ro&&"ResizeObserver"in window&&(this._ro=new window.ResizeObserver(t=>{for(const e of t){let t=!0;if(e.contentRect){const s=this.sizeFor(e.contentRect);s&&!this.hasRectChanged(s)&&(t=!1)}t&&this._resizeListener()}})),this._ro&&this._ro.observe(this._e)}sameInteger(t,e){return Math.round(t)===Math.round(e)}hasRectChanged(t){return!this._lastSize||!t||!(this.sameInteger(t.x,this._lastSize.x)&&this.sameInteger(t.y,this._lastSize.y)&&this.sameInteger(t.w,this._lastSize.w)&&this.sameInteger(t.h,this._lastSize.h))}isShowing(){return"not-showing"!==this._state}refresh(){this.isShowing()&&!this.pendingRefresh&&(this.pendingRefresh=Promise.resolve().then(()=>{this.isShowing()&&this.show(),delete this.pendingRefresh}))}show(){switch(this._state){case"unattached":break;case"showing":this.hide(),this._svg&&this.render(this._svg,!0);break;case"not-showing":this.attach(),this._svg&&this.render(this._svg,!1)}}hide(){if(this._svg)for(;this._svg.lastChild;)this._svg.removeChild(this._svg.lastChild);this._state="not-showing"}remove(){this._svg&&this._svg.parentElement&&this._svg.parentElement.removeChild(this._svg),this._svg=void 0,this._state="unattached",this.detachListeners()}render(t,e){const s=this.size();if(s){let i=this._config;e&&(i=JSON.parse(JSON.stringify(this._config)),i.animate=!1),p(t,s,i,this._animationGroupDelay,this._seed),this._lastSize=s,this._state="showing"}}size(){return this.sizeFor(this._e.getBoundingClientRect())}sizeFor(t){if(this._svg){const e=this._svg.getBoundingClientRect(),s=t;return{x:(s.x||s.left)-(e.x||e.left),y:(s.y||s.top)-(e.y||e.top),w:s.width,h:s.height}}return null}}exports.annotate=function(t,e){return new _(t,e)},exports.annotationGroup=function(t){let e=0;for(const s of t){const t=s;t._animationGroupDelay=e;e+=0===t.animationDuration?0:t.animationDuration||800}const s=[...t];return{show(){for(const t of s)t.show()},hide(){for(const t of s)t.hide()}}}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const t="http://www.w3.org/2000/svg";class e{constructor(t){this.seed=t}next(){return this.seed?(2**31-1&(this.seed=Math.imul(48271,this.seed)))/2**31:Math.random()}}function s(t,e,s,i,n){return{type:"path",ops:c(t,e,s,i,n)}}function i(t,e){return function(t,e,i){const n=(t||[]).length;if(n>2){const s=[];for(let e=0;e<n-1;e++)s.push(...c(t[e][0],t[e][1],t[e+1][0],t[e+1][1],i));return e&&s.push(...c(t[n-1][0],t[n-1][1],t[0][0],t[0][1],i)),{type:"path",ops:s}}return 2===n?s(t[0][0],t[0][1],t[1][0],t[1][1],i):{type:"path",ops:[]}}(t,!0,e)}function n(t,e,s,n,o){return i([[t,e],[t+s,e],[t+s,e+n],[t,e+n]],o)}function o(t,e,s,i,n){return function(t,e,s,i){const[n,o]=f(i.increment,t,e,i.rx,i.ry,1,i.increment*h(.1,h(.4,1,s),s),s);let r=l(n,null,s);if(!s.disableMultiStroke){const[n]=f(i.increment,t,e,i.rx,i.ry,1.5,0,s),o=l(n,null,s);r=r.concat(o)}return{estimatedPoints:o,opset:{type:"path",ops:r}}}(t,e,n,function(t,e,s){const i=Math.sqrt(2*Math.PI*Math.sqrt((Math.pow(t/2,2)+Math.pow(e/2,2))/2)),n=Math.max(s.curveStepCount,s.curveStepCount/Math.sqrt(200)*i),o=2*Math.PI/n;let r=Math.abs(t/2),h=Math.abs(e/2);const c=1-s.curveFitting;return r+=a(r*c,s),h+=a(h*c,s),{increment:o,rx:r,ry:h}}(s,i,n)).opset}function r(t){return t.randomizer||(t.randomizer=new e(t.seed||0)),t.randomizer.next()}function h(t,e,s,i=1){return s.roughness*i*(r(s)*(e-t)+t)}function a(t,e,s=1){return h(-t,t,e,s)}function c(t,e,s,i,n,o=!1){const r=o?n.disableMultiStrokeFill:n.disableMultiStroke,h=u(t,e,s,i,n,!0,!1);if(r)return h;const a=u(t,e,s,i,n,!0,!0);return h.concat(a)}function u(t,e,s,i,n,o,h){const c=Math.pow(t-s,2)+Math.pow(e-i,2),u=Math.sqrt(c);let l=1;l=u<200?1:u>500?.4:-.0016668*u+1.233334;let f=n.maxRandomnessOffset||0;f*f*100>c&&(f=u/10);const g=f/2,d=.2+.2*r(n);let p=n.bowing*n.maxRandomnessOffset*(i-e)/200,_=n.bowing*n.maxRandomnessOffset*(t-s)/200;p=a(p,n,l),_=a(_,n,l);const m=[],w=()=>a(g,n,l),v=()=>a(f,n,l);return o&&(h?m.push({op:"move",data:[t+w(),e+w()]}):m.push({op:"move",data:[t+a(f,n,l),e+a(f,n,l)]})),h?m.push({op:"bcurveTo",data:[p+t+(s-t)*d+w(),_+e+(i-e)*d+w(),p+t+2*(s-t)*d+w(),_+e+2*(i-e)*d+w(),s+w(),i+w()]}):m.push({op:"bcurveTo",data:[p+t+(s-t)*d+v(),_+e+(i-e)*d+v(),p+t+2*(s-t)*d+v(),_+e+2*(i-e)*d+v(),s+v(),i+v()]}),m}function l(t,e,s){const i=t.length,n=[];if(i>3){const o=[],r=1-s.curveTightness;n.push({op:"move",data:[t[1][0],t[1][1]]});for(let e=1;e+2<i;e++){const s=t[e];o[0]=[s[0],s[1]],o[1]=[s[0]+(r*t[e+1][0]-r*t[e-1][0])/6,s[1]+(r*t[e+1][1]-r*t[e-1][1])/6],o[2]=[t[e+1][0]+(r*t[e][0]-r*t[e+2][0])/6,t[e+1][1]+(r*t[e][1]-r*t[e+2][1])/6],o[3]=[t[e+1][0],t[e+1][1]],n.push({op:"bcurveTo",data:[o[1][0],o[1][1],o[2][0],o[2][1],o[3][0],o[3][1]]})}if(e&&2===e.length){const t=s.maxRandomnessOffset;n.push({op:"lineTo",data:[e[0]+a(t,s),e[1]+a(t,s)]})}}else 3===i?(n.push({op:"move",data:[t[1][0],t[1][1]]}),n.push({op:"bcurveTo",data:[t[1][0],t[1][1],t[2][0],t[2][1],t[2][0],t[2][1]]})):2===i&&n.push(...c(t[0][0],t[0][1],t[1][0],t[1][1],s));return n}function f(t,e,s,i,n,o,r,h){const c=[],u=[],l=a(.5,h)-Math.PI/2;u.push([a(o,h)+e+.9*i*Math.cos(l-t),a(o,h)+s+.9*n*Math.sin(l-t)]);for(let r=l;r<2*Math.PI+l-.01;r+=t){const t=[a(o,h)+e+i*Math.cos(r),a(o,h)+s+n*Math.sin(r)];c.push(t),u.push(t)}return u.push([a(o,h)+e+i*Math.cos(l+2*Math.PI+.5*r),a(o,h)+s+n*Math.sin(l+2*Math.PI+.5*r)]),u.push([a(o,h)+e+.98*i*Math.cos(l+r),a(o,h)+s+.98*n*Math.sin(l+r)]),u.push([a(o,h)+e+.9*i*Math.cos(l+.5*r),a(o,h)+s+.9*n*Math.sin(l+.5*r)]),[u,c]}function g(t,e){return{maxRandomnessOffset:2,roughness:"highlight"===t?3:1.5,bowing:1,stroke:"#000",strokeWidth:1.5,curveTightness:0,curveFitting:.95,curveStepCount:9,fillStyle:"hachure",fillWeight:-1,hachureAngle:-41,hachureGap:-1,dashOffset:-1,dashGap:-1,zigzagOffset:-1,combineNestedSvgPaths:!1,disableMultiStroke:"double"!==t,disableMultiStrokeFill:!1,seed:e}}function d(e,i,r,h,a){const c=[];let u=r.strokeWidth||2;const l=function(t){const e=t.padding;if(e||0===e){if("number"==typeof e)return[e,e,e,e];if(Array.isArray(e)){const t=e;if(t.length)switch(t.length){case 4:return[...t];case 1:return[t[0],t[0],t[0],t[0]];case 2:return[...t,...t];case 3:return[...t,t[1]];default:return[t[0],t[1],t[2],t[3]]}}}return[5,5,5,5]}(r),f=void 0===r.animate||!!r.animate,d=r.iterations||2;switch(r.type){case"underline":{const t=g("single",a),e=i.y+i.h+l[2];for(let n=0;n<d;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}case"strike-through":{const t=g("single",a),e=i.y+i.h/2;for(let n=0;n<d;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}case"box":{const t=g("single",a),e=i.x-l[3],s=i.y-l[0],o=i.w+(l[1]+l[3]),r=i.h+(l[0]+l[2]);for(let i=0;i<d;i++)c.push(n(e,s,o,r,t));break}case"crossed-off":{const t=g("single",a),e=i.x,n=i.y,o=e+i.w,r=n+i.h;for(let i=0;i<d;i++)i%2?c.push(s(o,r,e,n,t)):c.push(s(e,n,o,r,t));for(let i=0;i<d;i++)i%2?c.push(s(e,r,o,n,t)):c.push(s(o,n,e,r,t));break}case"circle":{const t=g("single",a),e=g("double",a),s=i.w+(l[1]+l[3]),n=i.h+(l[0]+l[2]),r=i.x-l[3]+s/2,h=i.y-l[0]+n/2,u=Math.floor(d/2),f=d-2*u;for(let t=0;t<u;t++)c.push(o(r,h,s,n,e));for(let e=0;e<f;e++)c.push(o(r,h,s,n,t));break}case"highlight":{const t=g("highlight",a);u=.95*i.h;const e=i.y+i.h/2;for(let n=0;n<d;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}}if(c.length){const s=function(t){const e=[];for(const s of t){let t="";for(const i of s.ops){const s=i.data;switch(i.op){case"move":t.trim()&&e.push(t.trim()),t=`M${s[0]} ${s[1]} `;break;case"bcurveTo":t+=`C${s[0]} ${s[1]}, ${s[2]} ${s[3]}, ${s[4]} ${s[5]} `;break;case"lineTo":t+=`L${s[0]} ${s[1]} `}}t.trim()&&e.push(t.trim())}return e}(c),i=[],n=[];let o=0;const a=0===r.animationDuration?0:r.animationDuration||800,l=(t,e,s)=>t.setAttribute(e,s);for(const h of s){const s=document.createElementNS(t,"path");if(l(s,"d",h),l(s,"fill","none"),l(s,"stroke",r.color||"currentColor"),l(s,"stroke-width",""+u),f){const t=s.getTotalLength();i.push(t),o+=t}e.appendChild(s),n.push(s)}if(f){let t=0;for(let e=0;e<n.length;e++){const s=n[e],r=i[e],c=o?a*(r/o):0,u=h+t,l=s.style;l.strokeDashoffset=""+r,l.strokeDasharray=""+r,l.animation=`rough-notation-dash ${c}ms ease-out ${u}ms forwards`,t+=c}}}}class p{constructor(t,e){this._state="unattached",this._resizing=!1,this._seed=Math.floor(Math.random()*2**31),this._lastSizes=[],this._animationDelay=0,this._resizeListener=()=>{this._resizing||(this._resizing=!0,setTimeout(()=>{this._resizing=!1,"showing"===this._state&&this.haveRectsChanged()&&this.show()},400))},this._e=t,this._config=JSON.parse(JSON.stringify(e)),this.attach()}get animate(){return this._config.animate}set animate(t){this._config.animate=t}get animationDuration(){return this._config.animationDuration}set animationDuration(t){this._config.animationDuration=t}get iterations(){return this._config.iterations}set iterations(t){this._config.iterations=t}get color(){return this._config.color}set color(t){this._config.color!==t&&(this._config.color=t,this.refresh())}get strokeWidth(){return this._config.strokeWidth}set strokeWidth(t){this._config.strokeWidth!==t&&(this._config.strokeWidth=t,this.refresh())}get padding(){return this._config.padding}set padding(t){this._config.padding!==t&&(this._config.padding=t,this.refresh())}attach(){if("unattached"===this._state&&this._e.parentElement){!function(){if(!window.__rno_kf_s){const t=window.__rno_kf_s=document.createElement("style");t.textContent="@keyframes rough-notation-dash { to { stroke-dashoffset: 0; } }",document.head.appendChild(t)}}();const e=this._svg=document.createElementNS(t,"svg");e.setAttribute("class","rough-annotation");const s=e.style;s.position="absolute",s.top="0",s.left="0",s.overflow="visible",s.pointerEvents="none",s.width="100px",s.height="100px";const i="highlight"===this._config.type;if(this._e.insertAdjacentElement(i?"beforebegin":"afterend",e),this._state="not-showing",i){const t=window.getComputedStyle(this._e).position;(!t||"static"===t)&&(this._e.style.position="relative")}this.attachListeners()}}detachListeners(){window.removeEventListener("resize",this._resizeListener),this._ro&&this._ro.unobserve(this._e)}attachListeners(){this.detachListeners(),window.addEventListener("resize",this._resizeListener,{passive:!0}),!this._ro&&"ResizeObserver"in window&&(this._ro=new window.ResizeObserver(t=>{for(const e of t)e.contentRect&&this._resizeListener()})),this._ro&&this._ro.observe(this._e)}haveRectsChanged(){if(this._lastSizes.length){const t=this.rects();if(t.length!==this._lastSizes.length)return!0;for(let e=0;e<t.length;e++)if(!this.isSameRect(t[e],this._lastSizes[e]))return!0}return!1}isSameRect(t,e){const s=(t,e)=>Math.round(t)===Math.round(e);return s(t.x,e.x)&&s(t.y,e.y)&&s(t.w,e.w)&&s(t.h,e.h)}isShowing(){return"not-showing"!==this._state}refresh(){this.isShowing()&&!this.pendingRefresh&&(this.pendingRefresh=Promise.resolve().then(()=>{this.isShowing()&&this.show(),delete this.pendingRefresh}))}show(){switch(this._state){case"unattached":break;case"showing":this.hide(),this._svg&&this.render(this._svg,!0);break;case"not-showing":this.attach(),this._svg&&this.render(this._svg,!1)}}hide(){if(this._svg)for(;this._svg.lastChild;)this._svg.removeChild(this._svg.lastChild);this._state="not-showing"}remove(){this._svg&&this._svg.parentElement&&this._svg.parentElement.removeChild(this._svg),this._svg=void 0,this._state="unattached",this.detachListeners()}render(t,e){let s=this._config;e&&(s=JSON.parse(JSON.stringify(this._config)),s.animate=!1);const i=this.rects();let n=0;i.forEach(t=>n+=t.w);const o=s.animationDuration||800;let r=0;for(let e=0;e<i.length;e++){const h=i[e];s.animationDuration=o*(h.w/n),d(t,i[e],s,r+this._animationDelay,this._seed),r+=s.animationDuration}this._lastSizes=i,this._state="showing"}rects(){const t=[];if(this._svg)if(this._config.multiline){const e=this._e.getClientRects();for(let s=0;s<e.length;s++)t.push(this.svgRect(this._svg,e[s]))}else t.push(this.svgRect(this._svg,this._e.getBoundingClientRect()));return t}svgRect(t,e){const s=t.getBoundingClientRect(),i=e;return{x:(i.x||i.left)-(s.x||s.left),y:(i.y||i.top)-(s.y||s.top),w:i.width,h:i.height}}}exports.annotate=function(t,e){return new p(t,e)},exports.annotationGroup=function(t){let e=0;for(const s of t){const t=s;t._animationDelay=e;e+=0===t.animationDuration?0:t.animationDuration||800}const s=[...t];return{show(){for(const t of s)t.show()},hide(){for(const t of s)t.hide()}}}; |
@@ -1,1 +0,1 @@ | ||
const t="http://www.w3.org/2000/svg";class e{constructor(t){this.seed=t}next(){return this.seed?(2**31-1&(this.seed=Math.imul(48271,this.seed)))/2**31:Math.random()}}function s(t,e,s,i,n){return{type:"path",ops:c(t,e,s,i,n)}}function i(t,e){return function(t,e,i){const n=(t||[]).length;if(n>2){const s=[];for(let e=0;e<n-1;e++)s.push(...c(t[e][0],t[e][1],t[e+1][0],t[e+1][1],i));return e&&s.push(...c(t[n-1][0],t[n-1][1],t[0][0],t[0][1],i)),{type:"path",ops:s}}return 2===n?s(t[0][0],t[0][1],t[1][0],t[1][1],i):{type:"path",ops:[]}}(t,!0,e)}function n(t,e,s,n,o){return i([[t,e],[t+s,e],[t+s,e+n],[t,e+n]],o)}function o(t,e,s,i,n){return function(t,e,s,i){const[n,o]=f(i.increment,t,e,i.rx,i.ry,1,i.increment*h(.1,h(.4,1,s),s),s);let r=l(n,null,s);if(!s.disableMultiStroke){const[n]=f(i.increment,t,e,i.rx,i.ry,1.5,0,s),o=l(n,null,s);r=r.concat(o)}return{estimatedPoints:o,opset:{type:"path",ops:r}}}(t,e,n,function(t,e,s){const i=Math.sqrt(2*Math.PI*Math.sqrt((Math.pow(t/2,2)+Math.pow(e/2,2))/2)),n=Math.max(s.curveStepCount,s.curveStepCount/Math.sqrt(200)*i),o=2*Math.PI/n;let r=Math.abs(t/2),h=Math.abs(e/2);const c=1-s.curveFitting;return r+=a(r*c,s),h+=a(h*c,s),{increment:o,rx:r,ry:h}}(s,i,n)).opset}function r(t){return t.randomizer||(t.randomizer=new e(t.seed||0)),t.randomizer.next()}function h(t,e,s,i=1){return s.roughness*i*(r(s)*(e-t)+t)}function a(t,e,s=1){return h(-t,t,e,s)}function c(t,e,s,i,n,o=!1){const r=o?n.disableMultiStrokeFill:n.disableMultiStroke,h=u(t,e,s,i,n,!0,!1);if(r)return h;const a=u(t,e,s,i,n,!0,!0);return h.concat(a)}function u(t,e,s,i,n,o,h){const c=Math.pow(t-s,2)+Math.pow(e-i,2),u=Math.sqrt(c);let l=1;l=u<200?1:u>500?.4:-.0016668*u+1.233334;let f=n.maxRandomnessOffset||0;f*f*100>c&&(f=u/10);const g=f/2,d=.2+.2*r(n);let p=n.bowing*n.maxRandomnessOffset*(i-e)/200,_=n.bowing*n.maxRandomnessOffset*(t-s)/200;p=a(p,n,l),_=a(_,n,l);const m=[],w=()=>a(g,n,l),v=()=>a(f,n,l);return o&&(h?m.push({op:"move",data:[t+w(),e+w()]}):m.push({op:"move",data:[t+a(f,n,l),e+a(f,n,l)]})),h?m.push({op:"bcurveTo",data:[p+t+(s-t)*d+w(),_+e+(i-e)*d+w(),p+t+2*(s-t)*d+w(),_+e+2*(i-e)*d+w(),s+w(),i+w()]}):m.push({op:"bcurveTo",data:[p+t+(s-t)*d+v(),_+e+(i-e)*d+v(),p+t+2*(s-t)*d+v(),_+e+2*(i-e)*d+v(),s+v(),i+v()]}),m}function l(t,e,s){const i=t.length,n=[];if(i>3){const o=[],r=1-s.curveTightness;n.push({op:"move",data:[t[1][0],t[1][1]]});for(let e=1;e+2<i;e++){const s=t[e];o[0]=[s[0],s[1]],o[1]=[s[0]+(r*t[e+1][0]-r*t[e-1][0])/6,s[1]+(r*t[e+1][1]-r*t[e-1][1])/6],o[2]=[t[e+1][0]+(r*t[e][0]-r*t[e+2][0])/6,t[e+1][1]+(r*t[e][1]-r*t[e+2][1])/6],o[3]=[t[e+1][0],t[e+1][1]],n.push({op:"bcurveTo",data:[o[1][0],o[1][1],o[2][0],o[2][1],o[3][0],o[3][1]]})}if(e&&2===e.length){const t=s.maxRandomnessOffset;n.push({op:"lineTo",data:[e[0]+a(t,s),e[1]+a(t,s)]})}}else 3===i?(n.push({op:"move",data:[t[1][0],t[1][1]]}),n.push({op:"bcurveTo",data:[t[1][0],t[1][1],t[2][0],t[2][1],t[2][0],t[2][1]]})):2===i&&n.push(...c(t[0][0],t[0][1],t[1][0],t[1][1],s));return n}function f(t,e,s,i,n,o,r,h){const c=[],u=[],l=a(.5,h)-Math.PI/2;u.push([a(o,h)+e+.9*i*Math.cos(l-t),a(o,h)+s+.9*n*Math.sin(l-t)]);for(let r=l;r<2*Math.PI+l-.01;r+=t){const t=[a(o,h)+e+i*Math.cos(r),a(o,h)+s+n*Math.sin(r)];c.push(t),u.push(t)}return u.push([a(o,h)+e+i*Math.cos(l+2*Math.PI+.5*r),a(o,h)+s+n*Math.sin(l+2*Math.PI+.5*r)]),u.push([a(o,h)+e+.98*i*Math.cos(l+r),a(o,h)+s+.98*n*Math.sin(l+r)]),u.push([a(o,h)+e+.9*i*Math.cos(l+.5*r),a(o,h)+s+.9*n*Math.sin(l+.5*r)]),[u,c]}const g={maxRandomnessOffset:2,roughness:1.5,bowing:1,stroke:"#000",strokeWidth:1.5,curveTightness:0,curveFitting:.95,curveStepCount:9,fillStyle:"hachure",fillWeight:-1,hachureAngle:-41,hachureGap:-1,dashOffset:-1,dashGap:-1,zigzagOffset:-1,seed:0,combineNestedSvgPaths:!1,disableMultiStroke:!1,disableMultiStrokeFill:!1};function d(t,e){const s=JSON.parse(JSON.stringify(g));switch(t){case"highlight":s.roughness=3,s.disableMultiStroke=!0;break;case"single":s.disableMultiStroke=!0}return s.seed=e,s}function p(e,i,r,h,a){const c=[];let u=r.strokeWidth||2;const l=function(t){const e=t.padding;if(e||0===e){if("number"==typeof e)return[e,e,e,e];if(Array.isArray(e)){const t=e;if(t.length)switch(t.length){case 4:return[...t];case 1:return[t[0],t[0],t[0],t[0]];case 2:return[...t,...t];case 3:return[...t,t[1]];default:return[t[0],t[1],t[2],t[3]]}}}return[5,5,5,5]}(r),f=void 0===r.animate||!!r.animate,g=r.iterations||2;switch(r.type){case"underline":{const t=d("single",a),e=i.y+i.h+l[2];for(let n=0;n<g;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}case"strike-through":{const t=d("single",a),e=i.y+i.h/2;for(let n=0;n<g;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}case"box":{const t=d("single",a),e=i.x-l[3],s=i.y-l[0],o=i.w+(l[1]+l[3]),r=i.h+(l[0]+l[2]);for(let i=0;i<g;i++)c.push(n(e,s,o,r,t));break}case"crossed-off":{const t=d("single",a),e=i.x,n=i.y,o=e+i.w,r=n+i.h;for(let i=0;i<g;i++)i%2?c.push(s(o,r,e,n,t)):c.push(s(e,n,o,r,t));for(let i=0;i<g;i++)i%2?c.push(s(e,r,o,n,t)):c.push(s(o,n,e,r,t));break}case"circle":{const t=d("single",a),e=d("double",a),s=i.w+(l[1]+l[3]),n=i.h+(l[0]+l[2]),r=i.x-l[3]+s/2,h=i.y-l[0]+n/2,u=Math.floor(g/2),f=g-2*u;for(let t=0;t<u;t++)c.push(o(r,h,s,n,e));for(let e=0;e<f;e++)c.push(o(r,h,s,n,t));break}case"highlight":{const t=d("highlight",a);u=.95*i.h;const e=i.y+i.h/2;for(let n=0;n<g;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}}if(c.length){const s=function(t){const e=[];for(const s of t){let t="";for(const i of s.ops){const s=i.data;switch(i.op){case"move":t.trim()&&e.push(t.trim()),t=`M${s[0]} ${s[1]} `;break;case"bcurveTo":t+=`C${s[0]} ${s[1]}, ${s[2]} ${s[3]}, ${s[4]} ${s[5]} `;break;case"lineTo":t+=`L${s[0]} ${s[1]} `}}t.trim()&&e.push(t.trim())}return e}(c),i=[],n=[];let o=0;const a=0===r.animationDuration?0:r.animationDuration||800,l=(0===r.animationDelay?0:r.animationDelay||0)+(h||0);for(const h of s){const s=document.createElementNS(t,"path");if(s.setAttribute("d",h),s.setAttribute("fill","none"),s.setAttribute("stroke",r.color||"currentColor"),s.setAttribute("stroke-width",""+u),f){const t=s.getTotalLength();i.push(t),o+=t}e.appendChild(s),n.push(s)}if(f){let t=0;for(let e=0;e<n.length;e++){const s=n[e],r=i[e],h=o?a*(r/o):0,c=l+t,u=s.style;u.strokeDashoffset=""+r,u.strokeDasharray=""+r,u.animation=`rough-notation-dash ${h}ms ease-out ${c}ms forwards`,t+=h}}}}class _{constructor(t,e){this._state="unattached",this._resizing=!1,this._seed=Math.floor(Math.random()*2**31),this._animationGroupDelay=0,this._resizeListener=()=>{this._resizing||(this._resizing=!0,setTimeout(()=>{if(this._resizing=!1,"showing"===this._state){const t=this.size();t&&this.hasRectChanged(t)&&this.show()}},400))},this._e=t,this._config=JSON.parse(JSON.stringify(e)),this.attach()}get animate(){return this._config.animate}set animate(t){this._config.animate=t}get animationDuration(){return this._config.animationDuration}set animationDuration(t){this._config.animationDuration=t}get animationDelay(){return this._config.animationDelay}set animationDelay(t){this._config.animationDelay=t}get iterations(){return this._config.iterations}set iterations(t){this._config.iterations=t}get color(){return this._config.color}set color(t){this._config.color!==t&&(this._config.color=t,this.refresh())}get strokeWidth(){return this._config.strokeWidth}set strokeWidth(t){this._config.strokeWidth!==t&&(this._config.strokeWidth=t,this.refresh())}get padding(){return this._config.padding}set padding(t){this._config.padding!==t&&(this._config.padding=t,this.refresh())}attach(){if("unattached"===this._state&&this._e.parentElement){!function(){if(!window.__rno_kf_s){const t=window.__rno_kf_s=document.createElement("style");t.textContent="@keyframes rough-notation-dash { to { stroke-dashoffset: 0; } }",document.head.appendChild(t)}}();const e=this._svg=document.createElementNS(t,"svg");e.setAttribute("class","rough-annotation");const s=e.style;s.position="absolute",s.top="0",s.left="0",s.overflow="visible",s.pointerEvents="none",s.width="100px",s.height="100px";const i="highlight"===this._config.type;if(this._e.insertAdjacentElement(i?"beforebegin":"afterend",e),this._state="not-showing",i){const t=window.getComputedStyle(this._e).position;(!t||"static"===t)&&(this._e.style.position="relative")}this.attachListeners()}}detachListeners(){window.removeEventListener("resize",this._resizeListener),this._ro&&this._ro.unobserve(this._e)}attachListeners(){this.detachListeners(),window.addEventListener("resize",this._resizeListener,{passive:!0}),!this._ro&&"ResizeObserver"in window&&(this._ro=new window.ResizeObserver(t=>{for(const e of t){let t=!0;if(e.contentRect){const s=this.sizeFor(e.contentRect);s&&!this.hasRectChanged(s)&&(t=!1)}t&&this._resizeListener()}})),this._ro&&this._ro.observe(this._e)}sameInteger(t,e){return Math.round(t)===Math.round(e)}hasRectChanged(t){return!this._lastSize||!t||!(this.sameInteger(t.x,this._lastSize.x)&&this.sameInteger(t.y,this._lastSize.y)&&this.sameInteger(t.w,this._lastSize.w)&&this.sameInteger(t.h,this._lastSize.h))}isShowing(){return"not-showing"!==this._state}refresh(){this.isShowing()&&!this.pendingRefresh&&(this.pendingRefresh=Promise.resolve().then(()=>{this.isShowing()&&this.show(),delete this.pendingRefresh}))}show(){switch(this._state){case"unattached":break;case"showing":this.hide(),this._svg&&this.render(this._svg,!0);break;case"not-showing":this.attach(),this._svg&&this.render(this._svg,!1)}}hide(){if(this._svg)for(;this._svg.lastChild;)this._svg.removeChild(this._svg.lastChild);this._state="not-showing"}remove(){this._svg&&this._svg.parentElement&&this._svg.parentElement.removeChild(this._svg),this._svg=void 0,this._state="unattached",this.detachListeners()}render(t,e){const s=this.size();if(s){let i=this._config;e&&(i=JSON.parse(JSON.stringify(this._config)),i.animate=!1),p(t,s,i,this._animationGroupDelay,this._seed),this._lastSize=s,this._state="showing"}}size(){return this.sizeFor(this._e.getBoundingClientRect())}sizeFor(t){if(this._svg){const e=this._svg.getBoundingClientRect(),s=t;return{x:(s.x||s.left)-(e.x||e.left),y:(s.y||s.top)-(e.y||e.top),w:s.width,h:s.height}}return null}}function m(t,e){return new _(t,e)}function w(t){let e=0;for(const s of t){const t=s;t._animationGroupDelay=e;e+=0===t.animationDuration?0:t.animationDuration||800}const s=[...t];return{show(){for(const t of s)t.show()},hide(){for(const t of s)t.hide()}}}export{m as annotate,w as annotationGroup}; | ||
const t="http://www.w3.org/2000/svg";class e{constructor(t){this.seed=t}next(){return this.seed?(2**31-1&(this.seed=Math.imul(48271,this.seed)))/2**31:Math.random()}}function s(t,e,s,i,n){return{type:"path",ops:c(t,e,s,i,n)}}function i(t,e){return function(t,e,i){const n=(t||[]).length;if(n>2){const s=[];for(let e=0;e<n-1;e++)s.push(...c(t[e][0],t[e][1],t[e+1][0],t[e+1][1],i));return e&&s.push(...c(t[n-1][0],t[n-1][1],t[0][0],t[0][1],i)),{type:"path",ops:s}}return 2===n?s(t[0][0],t[0][1],t[1][0],t[1][1],i):{type:"path",ops:[]}}(t,!0,e)}function n(t,e,s,n,o){return i([[t,e],[t+s,e],[t+s,e+n],[t,e+n]],o)}function o(t,e,s,i,n){return function(t,e,s,i){const[n,o]=f(i.increment,t,e,i.rx,i.ry,1,i.increment*r(.1,r(.4,1,s),s),s);let h=l(n,null,s);if(!s.disableMultiStroke){const[n]=f(i.increment,t,e,i.rx,i.ry,1.5,0,s),o=l(n,null,s);h=h.concat(o)}return{estimatedPoints:o,opset:{type:"path",ops:h}}}(t,e,n,function(t,e,s){const i=Math.sqrt(2*Math.PI*Math.sqrt((Math.pow(t/2,2)+Math.pow(e/2,2))/2)),n=Math.max(s.curveStepCount,s.curveStepCount/Math.sqrt(200)*i),o=2*Math.PI/n;let h=Math.abs(t/2),r=Math.abs(e/2);const c=1-s.curveFitting;return h+=a(h*c,s),r+=a(r*c,s),{increment:o,rx:h,ry:r}}(s,i,n)).opset}function h(t){return t.randomizer||(t.randomizer=new e(t.seed||0)),t.randomizer.next()}function r(t,e,s,i=1){return s.roughness*i*(h(s)*(e-t)+t)}function a(t,e,s=1){return r(-t,t,e,s)}function c(t,e,s,i,n,o=!1){const h=o?n.disableMultiStrokeFill:n.disableMultiStroke,r=u(t,e,s,i,n,!0,!1);if(h)return r;const a=u(t,e,s,i,n,!0,!0);return r.concat(a)}function u(t,e,s,i,n,o,r){const c=Math.pow(t-s,2)+Math.pow(e-i,2),u=Math.sqrt(c);let l=1;l=u<200?1:u>500?.4:-.0016668*u+1.233334;let f=n.maxRandomnessOffset||0;f*f*100>c&&(f=u/10);const g=f/2,d=.2+.2*h(n);let p=n.bowing*n.maxRandomnessOffset*(i-e)/200,_=n.bowing*n.maxRandomnessOffset*(t-s)/200;p=a(p,n,l),_=a(_,n,l);const m=[],w=()=>a(g,n,l),v=()=>a(f,n,l);return o&&(r?m.push({op:"move",data:[t+w(),e+w()]}):m.push({op:"move",data:[t+a(f,n,l),e+a(f,n,l)]})),r?m.push({op:"bcurveTo",data:[p+t+(s-t)*d+w(),_+e+(i-e)*d+w(),p+t+2*(s-t)*d+w(),_+e+2*(i-e)*d+w(),s+w(),i+w()]}):m.push({op:"bcurveTo",data:[p+t+(s-t)*d+v(),_+e+(i-e)*d+v(),p+t+2*(s-t)*d+v(),_+e+2*(i-e)*d+v(),s+v(),i+v()]}),m}function l(t,e,s){const i=t.length,n=[];if(i>3){const o=[],h=1-s.curveTightness;n.push({op:"move",data:[t[1][0],t[1][1]]});for(let e=1;e+2<i;e++){const s=t[e];o[0]=[s[0],s[1]],o[1]=[s[0]+(h*t[e+1][0]-h*t[e-1][0])/6,s[1]+(h*t[e+1][1]-h*t[e-1][1])/6],o[2]=[t[e+1][0]+(h*t[e][0]-h*t[e+2][0])/6,t[e+1][1]+(h*t[e][1]-h*t[e+2][1])/6],o[3]=[t[e+1][0],t[e+1][1]],n.push({op:"bcurveTo",data:[o[1][0],o[1][1],o[2][0],o[2][1],o[3][0],o[3][1]]})}if(e&&2===e.length){const t=s.maxRandomnessOffset;n.push({op:"lineTo",data:[e[0]+a(t,s),e[1]+a(t,s)]})}}else 3===i?(n.push({op:"move",data:[t[1][0],t[1][1]]}),n.push({op:"bcurveTo",data:[t[1][0],t[1][1],t[2][0],t[2][1],t[2][0],t[2][1]]})):2===i&&n.push(...c(t[0][0],t[0][1],t[1][0],t[1][1],s));return n}function f(t,e,s,i,n,o,h,r){const c=[],u=[],l=a(.5,r)-Math.PI/2;u.push([a(o,r)+e+.9*i*Math.cos(l-t),a(o,r)+s+.9*n*Math.sin(l-t)]);for(let h=l;h<2*Math.PI+l-.01;h+=t){const t=[a(o,r)+e+i*Math.cos(h),a(o,r)+s+n*Math.sin(h)];c.push(t),u.push(t)}return u.push([a(o,r)+e+i*Math.cos(l+2*Math.PI+.5*h),a(o,r)+s+n*Math.sin(l+2*Math.PI+.5*h)]),u.push([a(o,r)+e+.98*i*Math.cos(l+h),a(o,r)+s+.98*n*Math.sin(l+h)]),u.push([a(o,r)+e+.9*i*Math.cos(l+.5*h),a(o,r)+s+.9*n*Math.sin(l+.5*h)]),[u,c]}function g(t,e){return{maxRandomnessOffset:2,roughness:"highlight"===t?3:1.5,bowing:1,stroke:"#000",strokeWidth:1.5,curveTightness:0,curveFitting:.95,curveStepCount:9,fillStyle:"hachure",fillWeight:-1,hachureAngle:-41,hachureGap:-1,dashOffset:-1,dashGap:-1,zigzagOffset:-1,combineNestedSvgPaths:!1,disableMultiStroke:"double"!==t,disableMultiStrokeFill:!1,seed:e}}function d(e,i,h,r,a){const c=[];let u=h.strokeWidth||2;const l=function(t){const e=t.padding;if(e||0===e){if("number"==typeof e)return[e,e,e,e];if(Array.isArray(e)){const t=e;if(t.length)switch(t.length){case 4:return[...t];case 1:return[t[0],t[0],t[0],t[0]];case 2:return[...t,...t];case 3:return[...t,t[1]];default:return[t[0],t[1],t[2],t[3]]}}}return[5,5,5,5]}(h),f=void 0===h.animate||!!h.animate,d=h.iterations||2;switch(h.type){case"underline":{const t=g("single",a),e=i.y+i.h+l[2];for(let n=0;n<d;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}case"strike-through":{const t=g("single",a),e=i.y+i.h/2;for(let n=0;n<d;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}case"box":{const t=g("single",a),e=i.x-l[3],s=i.y-l[0],o=i.w+(l[1]+l[3]),h=i.h+(l[0]+l[2]);for(let i=0;i<d;i++)c.push(n(e,s,o,h,t));break}case"crossed-off":{const t=g("single",a),e=i.x,n=i.y,o=e+i.w,h=n+i.h;for(let i=0;i<d;i++)i%2?c.push(s(o,h,e,n,t)):c.push(s(e,n,o,h,t));for(let i=0;i<d;i++)i%2?c.push(s(e,h,o,n,t)):c.push(s(o,n,e,h,t));break}case"circle":{const t=g("single",a),e=g("double",a),s=i.w+(l[1]+l[3]),n=i.h+(l[0]+l[2]),h=i.x-l[3]+s/2,r=i.y-l[0]+n/2,u=Math.floor(d/2),f=d-2*u;for(let t=0;t<u;t++)c.push(o(h,r,s,n,e));for(let e=0;e<f;e++)c.push(o(h,r,s,n,t));break}case"highlight":{const t=g("highlight",a);u=.95*i.h;const e=i.y+i.h/2;for(let n=0;n<d;n++)n%2?c.push(s(i.x+i.w,e,i.x,e,t)):c.push(s(i.x,e,i.x+i.w,e,t));break}}if(c.length){const s=function(t){const e=[];for(const s of t){let t="";for(const i of s.ops){const s=i.data;switch(i.op){case"move":t.trim()&&e.push(t.trim()),t=`M${s[0]} ${s[1]} `;break;case"bcurveTo":t+=`C${s[0]} ${s[1]}, ${s[2]} ${s[3]}, ${s[4]} ${s[5]} `;break;case"lineTo":t+=`L${s[0]} ${s[1]} `}}t.trim()&&e.push(t.trim())}return e}(c),i=[],n=[];let o=0;const a=0===h.animationDuration?0:h.animationDuration||800,l=(t,e,s)=>t.setAttribute(e,s);for(const r of s){const s=document.createElementNS(t,"path");if(l(s,"d",r),l(s,"fill","none"),l(s,"stroke",h.color||"currentColor"),l(s,"stroke-width",""+u),f){const t=s.getTotalLength();i.push(t),o+=t}e.appendChild(s),n.push(s)}if(f){let t=0;for(let e=0;e<n.length;e++){const s=n[e],h=i[e],c=o?a*(h/o):0,u=r+t,l=s.style;l.strokeDashoffset=""+h,l.strokeDasharray=""+h,l.animation=`rough-notation-dash ${c}ms ease-out ${u}ms forwards`,t+=c}}}}class p{constructor(t,e){this._state="unattached",this._resizing=!1,this._seed=Math.floor(Math.random()*2**31),this._lastSizes=[],this._animationDelay=0,this._resizeListener=()=>{this._resizing||(this._resizing=!0,setTimeout(()=>{this._resizing=!1,"showing"===this._state&&this.haveRectsChanged()&&this.show()},400))},this._e=t,this._config=JSON.parse(JSON.stringify(e)),this.attach()}get animate(){return this._config.animate}set animate(t){this._config.animate=t}get animationDuration(){return this._config.animationDuration}set animationDuration(t){this._config.animationDuration=t}get iterations(){return this._config.iterations}set iterations(t){this._config.iterations=t}get color(){return this._config.color}set color(t){this._config.color!==t&&(this._config.color=t,this.refresh())}get strokeWidth(){return this._config.strokeWidth}set strokeWidth(t){this._config.strokeWidth!==t&&(this._config.strokeWidth=t,this.refresh())}get padding(){return this._config.padding}set padding(t){this._config.padding!==t&&(this._config.padding=t,this.refresh())}attach(){if("unattached"===this._state&&this._e.parentElement){!function(){if(!window.__rno_kf_s){const t=window.__rno_kf_s=document.createElement("style");t.textContent="@keyframes rough-notation-dash { to { stroke-dashoffset: 0; } }",document.head.appendChild(t)}}();const e=this._svg=document.createElementNS(t,"svg");e.setAttribute("class","rough-annotation");const s=e.style;s.position="absolute",s.top="0",s.left="0",s.overflow="visible",s.pointerEvents="none",s.width="100px",s.height="100px";const i="highlight"===this._config.type;if(this._e.insertAdjacentElement(i?"beforebegin":"afterend",e),this._state="not-showing",i){const t=window.getComputedStyle(this._e).position;(!t||"static"===t)&&(this._e.style.position="relative")}this.attachListeners()}}detachListeners(){window.removeEventListener("resize",this._resizeListener),this._ro&&this._ro.unobserve(this._e)}attachListeners(){this.detachListeners(),window.addEventListener("resize",this._resizeListener,{passive:!0}),!this._ro&&"ResizeObserver"in window&&(this._ro=new window.ResizeObserver(t=>{for(const e of t)e.contentRect&&this._resizeListener()})),this._ro&&this._ro.observe(this._e)}haveRectsChanged(){if(this._lastSizes.length){const t=this.rects();if(t.length!==this._lastSizes.length)return!0;for(let e=0;e<t.length;e++)if(!this.isSameRect(t[e],this._lastSizes[e]))return!0}return!1}isSameRect(t,e){const s=(t,e)=>Math.round(t)===Math.round(e);return s(t.x,e.x)&&s(t.y,e.y)&&s(t.w,e.w)&&s(t.h,e.h)}isShowing(){return"not-showing"!==this._state}refresh(){this.isShowing()&&!this.pendingRefresh&&(this.pendingRefresh=Promise.resolve().then(()=>{this.isShowing()&&this.show(),delete this.pendingRefresh}))}show(){switch(this._state){case"unattached":break;case"showing":this.hide(),this._svg&&this.render(this._svg,!0);break;case"not-showing":this.attach(),this._svg&&this.render(this._svg,!1)}}hide(){if(this._svg)for(;this._svg.lastChild;)this._svg.removeChild(this._svg.lastChild);this._state="not-showing"}remove(){this._svg&&this._svg.parentElement&&this._svg.parentElement.removeChild(this._svg),this._svg=void 0,this._state="unattached",this.detachListeners()}render(t,e){let s=this._config;e&&(s=JSON.parse(JSON.stringify(this._config)),s.animate=!1);const i=this.rects();let n=0;i.forEach(t=>n+=t.w);const o=s.animationDuration||800;let h=0;for(let e=0;e<i.length;e++){const r=i[e];s.animationDuration=o*(r.w/n),d(t,i[e],s,h+this._animationDelay,this._seed),h+=s.animationDuration}this._lastSizes=i,this._state="showing"}rects(){const t=[];if(this._svg)if(this._config.multiline){const e=this._e.getClientRects();for(let s=0;s<e.length;s++)t.push(this.svgRect(this._svg,e[s]))}else t.push(this.svgRect(this._svg,this._e.getBoundingClientRect()));return t}svgRect(t,e){const s=t.getBoundingClientRect(),i=e;return{x:(i.x||i.left)-(s.x||s.left),y:(i.y||i.top)-(s.y||s.top),w:i.width,h:i.height}}}function _(t,e){return new p(t,e)}function m(t){let e=0;for(const s of t){const t=s;t._animationDelay=e;e+=0===t.animationDuration?0:t.animationDuration||800}const s=[...t];return{show(){for(const t of s)t.show()},hide(){for(const t of s)t.hide()}}}export{_ as annotate,m as annotationGroup}; |
@@ -1,1 +0,1 @@ | ||
var RoughNotation=function(t){"use strict";const e="http://www.w3.org/2000/svg";class s{constructor(t){this.seed=t}next(){return this.seed?(2**31-1&(this.seed=Math.imul(48271,this.seed)))/2**31:Math.random()}}function i(t,e,s,i,n){return{type:"path",ops:u(t,e,s,i,n)}}function n(t,e){return function(t,e,s){const n=(t||[]).length;if(n>2){const i=[];for(let e=0;e<n-1;e++)i.push(...u(t[e][0],t[e][1],t[e+1][0],t[e+1][1],s));return e&&i.push(...u(t[n-1][0],t[n-1][1],t[0][0],t[0][1],s)),{type:"path",ops:i}}return 2===n?i(t[0][0],t[0][1],t[1][0],t[1][1],s):{type:"path",ops:[]}}(t,!0,e)}function o(t,e,s,i,o){return n([[t,e],[t+s,e],[t+s,e+i],[t,e+i]],o)}function r(t,e,s,i,n){return function(t,e,s,i){const[n,o]=g(i.increment,t,e,i.rx,i.ry,1,i.increment*a(.1,a(.4,1,s),s),s);let r=f(n,null,s);if(!s.disableMultiStroke){const[n]=g(i.increment,t,e,i.rx,i.ry,1.5,0,s),o=f(n,null,s);r=r.concat(o)}return{estimatedPoints:o,opset:{type:"path",ops:r}}}(t,e,n,function(t,e,s){const i=Math.sqrt(2*Math.PI*Math.sqrt((Math.pow(t/2,2)+Math.pow(e/2,2))/2)),n=Math.max(s.curveStepCount,s.curveStepCount/Math.sqrt(200)*i),o=2*Math.PI/n;let r=Math.abs(t/2),h=Math.abs(e/2);const a=1-s.curveFitting;return r+=c(r*a,s),h+=c(h*a,s),{increment:o,rx:r,ry:h}}(s,i,n)).opset}function h(t){return t.randomizer||(t.randomizer=new s(t.seed||0)),t.randomizer.next()}function a(t,e,s,i=1){return s.roughness*i*(h(s)*(e-t)+t)}function c(t,e,s=1){return a(-t,t,e,s)}function u(t,e,s,i,n,o=!1){const r=o?n.disableMultiStrokeFill:n.disableMultiStroke,h=l(t,e,s,i,n,!0,!1);if(r)return h;const a=l(t,e,s,i,n,!0,!0);return h.concat(a)}function l(t,e,s,i,n,o,r){const a=Math.pow(t-s,2)+Math.pow(e-i,2),u=Math.sqrt(a);let l=1;l=u<200?1:u>500?.4:-.0016668*u+1.233334;let f=n.maxRandomnessOffset||0;f*f*100>a&&(f=u/10);const g=f/2,d=.2+.2*h(n);let p=n.bowing*n.maxRandomnessOffset*(i-e)/200,_=n.bowing*n.maxRandomnessOffset*(t-s)/200;p=c(p,n,l),_=c(_,n,l);const m=[],w=()=>c(g,n,l),v=()=>c(f,n,l);return o&&(r?m.push({op:"move",data:[t+w(),e+w()]}):m.push({op:"move",data:[t+c(f,n,l),e+c(f,n,l)]})),r?m.push({op:"bcurveTo",data:[p+t+(s-t)*d+w(),_+e+(i-e)*d+w(),p+t+2*(s-t)*d+w(),_+e+2*(i-e)*d+w(),s+w(),i+w()]}):m.push({op:"bcurveTo",data:[p+t+(s-t)*d+v(),_+e+(i-e)*d+v(),p+t+2*(s-t)*d+v(),_+e+2*(i-e)*d+v(),s+v(),i+v()]}),m}function f(t,e,s){const i=t.length,n=[];if(i>3){const o=[],r=1-s.curveTightness;n.push({op:"move",data:[t[1][0],t[1][1]]});for(let e=1;e+2<i;e++){const s=t[e];o[0]=[s[0],s[1]],o[1]=[s[0]+(r*t[e+1][0]-r*t[e-1][0])/6,s[1]+(r*t[e+1][1]-r*t[e-1][1])/6],o[2]=[t[e+1][0]+(r*t[e][0]-r*t[e+2][0])/6,t[e+1][1]+(r*t[e][1]-r*t[e+2][1])/6],o[3]=[t[e+1][0],t[e+1][1]],n.push({op:"bcurveTo",data:[o[1][0],o[1][1],o[2][0],o[2][1],o[3][0],o[3][1]]})}if(e&&2===e.length){const t=s.maxRandomnessOffset;n.push({op:"lineTo",data:[e[0]+c(t,s),e[1]+c(t,s)]})}}else 3===i?(n.push({op:"move",data:[t[1][0],t[1][1]]}),n.push({op:"bcurveTo",data:[t[1][0],t[1][1],t[2][0],t[2][1],t[2][0],t[2][1]]})):2===i&&n.push(...u(t[0][0],t[0][1],t[1][0],t[1][1],s));return n}function g(t,e,s,i,n,o,r,h){const a=[],u=[],l=c(.5,h)-Math.PI/2;u.push([c(o,h)+e+.9*i*Math.cos(l-t),c(o,h)+s+.9*n*Math.sin(l-t)]);for(let r=l;r<2*Math.PI+l-.01;r+=t){const t=[c(o,h)+e+i*Math.cos(r),c(o,h)+s+n*Math.sin(r)];a.push(t),u.push(t)}return u.push([c(o,h)+e+i*Math.cos(l+2*Math.PI+.5*r),c(o,h)+s+n*Math.sin(l+2*Math.PI+.5*r)]),u.push([c(o,h)+e+.98*i*Math.cos(l+r),c(o,h)+s+.98*n*Math.sin(l+r)]),u.push([c(o,h)+e+.9*i*Math.cos(l+.5*r),c(o,h)+s+.9*n*Math.sin(l+.5*r)]),[u,a]}const d={maxRandomnessOffset:2,roughness:1.5,bowing:1,stroke:"#000",strokeWidth:1.5,curveTightness:0,curveFitting:.95,curveStepCount:9,fillStyle:"hachure",fillWeight:-1,hachureAngle:-41,hachureGap:-1,dashOffset:-1,dashGap:-1,zigzagOffset:-1,seed:0,combineNestedSvgPaths:!1,disableMultiStroke:!1,disableMultiStrokeFill:!1};function p(t,e){const s=JSON.parse(JSON.stringify(d));switch(t){case"highlight":s.roughness=3,s.disableMultiStroke=!0;break;case"single":s.disableMultiStroke=!0}return s.seed=e,s}function _(t,s,n,h,a){const c=[];let u=n.strokeWidth||2;const l=function(t){const e=t.padding;if(e||0===e){if("number"==typeof e)return[e,e,e,e];if(Array.isArray(e)){const t=e;if(t.length)switch(t.length){case 4:return[...t];case 1:return[t[0],t[0],t[0],t[0]];case 2:return[...t,...t];case 3:return[...t,t[1]];default:return[t[0],t[1],t[2],t[3]]}}}return[5,5,5,5]}(n),f=void 0===n.animate||!!n.animate,g=n.iterations||2;switch(n.type){case"underline":{const t=p("single",a),e=s.y+s.h+l[2];for(let n=0;n<g;n++)n%2?c.push(i(s.x+s.w,e,s.x,e,t)):c.push(i(s.x,e,s.x+s.w,e,t));break}case"strike-through":{const t=p("single",a),e=s.y+s.h/2;for(let n=0;n<g;n++)n%2?c.push(i(s.x+s.w,e,s.x,e,t)):c.push(i(s.x,e,s.x+s.w,e,t));break}case"box":{const t=p("single",a),e=s.x-l[3],i=s.y-l[0],n=s.w+(l[1]+l[3]),r=s.h+(l[0]+l[2]);for(let s=0;s<g;s++)c.push(o(e,i,n,r,t));break}case"crossed-off":{const t=p("single",a),e=s.x,n=s.y,o=e+s.w,r=n+s.h;for(let s=0;s<g;s++)s%2?c.push(i(o,r,e,n,t)):c.push(i(e,n,o,r,t));for(let s=0;s<g;s++)s%2?c.push(i(e,r,o,n,t)):c.push(i(o,n,e,r,t));break}case"circle":{const t=p("single",a),e=p("double",a),i=s.w+(l[1]+l[3]),n=s.h+(l[0]+l[2]),o=s.x-l[3]+i/2,h=s.y-l[0]+n/2,u=Math.floor(g/2),f=g-2*u;for(let t=0;t<u;t++)c.push(r(o,h,i,n,e));for(let e=0;e<f;e++)c.push(r(o,h,i,n,t));break}case"highlight":{const t=p("highlight",a);u=.95*s.h;const e=s.y+s.h/2;for(let n=0;n<g;n++)n%2?c.push(i(s.x+s.w,e,s.x,e,t)):c.push(i(s.x,e,s.x+s.w,e,t));break}}if(c.length){const s=function(t){const e=[];for(const s of t){let t="";for(const i of s.ops){const s=i.data;switch(i.op){case"move":t.trim()&&e.push(t.trim()),t=`M${s[0]} ${s[1]} `;break;case"bcurveTo":t+=`C${s[0]} ${s[1]}, ${s[2]} ${s[3]}, ${s[4]} ${s[5]} `;break;case"lineTo":t+=`L${s[0]} ${s[1]} `}}t.trim()&&e.push(t.trim())}return e}(c),i=[],o=[];let r=0;const a=0===n.animationDuration?0:n.animationDuration||800,l=(0===n.animationDelay?0:n.animationDelay||0)+(h||0);for(const h of s){const s=document.createElementNS(e,"path");if(s.setAttribute("d",h),s.setAttribute("fill","none"),s.setAttribute("stroke",n.color||"currentColor"),s.setAttribute("stroke-width",""+u),f){const t=s.getTotalLength();i.push(t),r+=t}t.appendChild(s),o.push(s)}if(f){let t=0;for(let e=0;e<o.length;e++){const s=o[e],n=i[e],h=r?a*(n/r):0,c=l+t,u=s.style;u.strokeDashoffset=""+n,u.strokeDasharray=""+n,u.animation=`rough-notation-dash ${h}ms ease-out ${c}ms forwards`,t+=h}}}}class m{constructor(t,e){this._state="unattached",this._resizing=!1,this._seed=Math.floor(Math.random()*2**31),this._animationGroupDelay=0,this._resizeListener=()=>{this._resizing||(this._resizing=!0,setTimeout(()=>{if(this._resizing=!1,"showing"===this._state){const t=this.size();t&&this.hasRectChanged(t)&&this.show()}},400))},this._e=t,this._config=JSON.parse(JSON.stringify(e)),this.attach()}get animate(){return this._config.animate}set animate(t){this._config.animate=t}get animationDuration(){return this._config.animationDuration}set animationDuration(t){this._config.animationDuration=t}get animationDelay(){return this._config.animationDelay}set animationDelay(t){this._config.animationDelay=t}get iterations(){return this._config.iterations}set iterations(t){this._config.iterations=t}get color(){return this._config.color}set color(t){this._config.color!==t&&(this._config.color=t,this.refresh())}get strokeWidth(){return this._config.strokeWidth}set strokeWidth(t){this._config.strokeWidth!==t&&(this._config.strokeWidth=t,this.refresh())}get padding(){return this._config.padding}set padding(t){this._config.padding!==t&&(this._config.padding=t,this.refresh())}attach(){if("unattached"===this._state&&this._e.parentElement){!function(){if(!window.__rno_kf_s){const t=window.__rno_kf_s=document.createElement("style");t.textContent="@keyframes rough-notation-dash { to { stroke-dashoffset: 0; } }",document.head.appendChild(t)}}();const t=this._svg=document.createElementNS(e,"svg");t.setAttribute("class","rough-annotation");const s=t.style;s.position="absolute",s.top="0",s.left="0",s.overflow="visible",s.pointerEvents="none",s.width="100px",s.height="100px";const i="highlight"===this._config.type;if(this._e.insertAdjacentElement(i?"beforebegin":"afterend",t),this._state="not-showing",i){const t=window.getComputedStyle(this._e).position;(!t||"static"===t)&&(this._e.style.position="relative")}this.attachListeners()}}detachListeners(){window.removeEventListener("resize",this._resizeListener),this._ro&&this._ro.unobserve(this._e)}attachListeners(){this.detachListeners(),window.addEventListener("resize",this._resizeListener,{passive:!0}),!this._ro&&"ResizeObserver"in window&&(this._ro=new window.ResizeObserver(t=>{for(const e of t){let t=!0;if(e.contentRect){const s=this.sizeFor(e.contentRect);s&&!this.hasRectChanged(s)&&(t=!1)}t&&this._resizeListener()}})),this._ro&&this._ro.observe(this._e)}sameInteger(t,e){return Math.round(t)===Math.round(e)}hasRectChanged(t){return!this._lastSize||!t||!(this.sameInteger(t.x,this._lastSize.x)&&this.sameInteger(t.y,this._lastSize.y)&&this.sameInteger(t.w,this._lastSize.w)&&this.sameInteger(t.h,this._lastSize.h))}isShowing(){return"not-showing"!==this._state}refresh(){this.isShowing()&&!this.pendingRefresh&&(this.pendingRefresh=Promise.resolve().then(()=>{this.isShowing()&&this.show(),delete this.pendingRefresh}))}show(){switch(this._state){case"unattached":break;case"showing":this.hide(),this._svg&&this.render(this._svg,!0);break;case"not-showing":this.attach(),this._svg&&this.render(this._svg,!1)}}hide(){if(this._svg)for(;this._svg.lastChild;)this._svg.removeChild(this._svg.lastChild);this._state="not-showing"}remove(){this._svg&&this._svg.parentElement&&this._svg.parentElement.removeChild(this._svg),this._svg=void 0,this._state="unattached",this.detachListeners()}render(t,e){const s=this.size();if(s){let i=this._config;e&&(i=JSON.parse(JSON.stringify(this._config)),i.animate=!1),_(t,s,i,this._animationGroupDelay,this._seed),this._lastSize=s,this._state="showing"}}size(){return this.sizeFor(this._e.getBoundingClientRect())}sizeFor(t){if(this._svg){const e=this._svg.getBoundingClientRect(),s=t;return{x:(s.x||s.left)-(e.x||e.left),y:(s.y||s.top)-(e.y||e.top),w:s.width,h:s.height}}return null}}return t.annotate=function(t,e){return new m(t,e)},t.annotationGroup=function(t){let e=0;for(const s of t){const t=s;t._animationGroupDelay=e;e+=0===t.animationDuration?0:t.animationDuration||800}const s=[...t];return{show(){for(const t of s)t.show()},hide(){for(const t of s)t.hide()}}},t}({}); | ||
var RoughNotation=function(t){"use strict";const e="http://www.w3.org/2000/svg";class s{constructor(t){this.seed=t}next(){return this.seed?(2**31-1&(this.seed=Math.imul(48271,this.seed)))/2**31:Math.random()}}function i(t,e,s,i,n){return{type:"path",ops:u(t,e,s,i,n)}}function n(t,e){return function(t,e,s){const n=(t||[]).length;if(n>2){const i=[];for(let e=0;e<n-1;e++)i.push(...u(t[e][0],t[e][1],t[e+1][0],t[e+1][1],s));return e&&i.push(...u(t[n-1][0],t[n-1][1],t[0][0],t[0][1],s)),{type:"path",ops:i}}return 2===n?i(t[0][0],t[0][1],t[1][0],t[1][1],s):{type:"path",ops:[]}}(t,!0,e)}function o(t,e,s,i,o){return n([[t,e],[t+s,e],[t+s,e+i],[t,e+i]],o)}function h(t,e,s,i,n){return function(t,e,s,i){const[n,o]=g(i.increment,t,e,i.rx,i.ry,1,i.increment*a(.1,a(.4,1,s),s),s);let h=f(n,null,s);if(!s.disableMultiStroke){const[n]=g(i.increment,t,e,i.rx,i.ry,1.5,0,s),o=f(n,null,s);h=h.concat(o)}return{estimatedPoints:o,opset:{type:"path",ops:h}}}(t,e,n,function(t,e,s){const i=Math.sqrt(2*Math.PI*Math.sqrt((Math.pow(t/2,2)+Math.pow(e/2,2))/2)),n=Math.max(s.curveStepCount,s.curveStepCount/Math.sqrt(200)*i),o=2*Math.PI/n;let h=Math.abs(t/2),r=Math.abs(e/2);const a=1-s.curveFitting;return h+=c(h*a,s),r+=c(r*a,s),{increment:o,rx:h,ry:r}}(s,i,n)).opset}function r(t){return t.randomizer||(t.randomizer=new s(t.seed||0)),t.randomizer.next()}function a(t,e,s,i=1){return s.roughness*i*(r(s)*(e-t)+t)}function c(t,e,s=1){return a(-t,t,e,s)}function u(t,e,s,i,n,o=!1){const h=o?n.disableMultiStrokeFill:n.disableMultiStroke,r=l(t,e,s,i,n,!0,!1);if(h)return r;const a=l(t,e,s,i,n,!0,!0);return r.concat(a)}function l(t,e,s,i,n,o,h){const a=Math.pow(t-s,2)+Math.pow(e-i,2),u=Math.sqrt(a);let l=1;l=u<200?1:u>500?.4:-.0016668*u+1.233334;let f=n.maxRandomnessOffset||0;f*f*100>a&&(f=u/10);const g=f/2,d=.2+.2*r(n);let p=n.bowing*n.maxRandomnessOffset*(i-e)/200,_=n.bowing*n.maxRandomnessOffset*(t-s)/200;p=c(p,n,l),_=c(_,n,l);const m=[],w=()=>c(g,n,l),v=()=>c(f,n,l);return o&&(h?m.push({op:"move",data:[t+w(),e+w()]}):m.push({op:"move",data:[t+c(f,n,l),e+c(f,n,l)]})),h?m.push({op:"bcurveTo",data:[p+t+(s-t)*d+w(),_+e+(i-e)*d+w(),p+t+2*(s-t)*d+w(),_+e+2*(i-e)*d+w(),s+w(),i+w()]}):m.push({op:"bcurveTo",data:[p+t+(s-t)*d+v(),_+e+(i-e)*d+v(),p+t+2*(s-t)*d+v(),_+e+2*(i-e)*d+v(),s+v(),i+v()]}),m}function f(t,e,s){const i=t.length,n=[];if(i>3){const o=[],h=1-s.curveTightness;n.push({op:"move",data:[t[1][0],t[1][1]]});for(let e=1;e+2<i;e++){const s=t[e];o[0]=[s[0],s[1]],o[1]=[s[0]+(h*t[e+1][0]-h*t[e-1][0])/6,s[1]+(h*t[e+1][1]-h*t[e-1][1])/6],o[2]=[t[e+1][0]+(h*t[e][0]-h*t[e+2][0])/6,t[e+1][1]+(h*t[e][1]-h*t[e+2][1])/6],o[3]=[t[e+1][0],t[e+1][1]],n.push({op:"bcurveTo",data:[o[1][0],o[1][1],o[2][0],o[2][1],o[3][0],o[3][1]]})}if(e&&2===e.length){const t=s.maxRandomnessOffset;n.push({op:"lineTo",data:[e[0]+c(t,s),e[1]+c(t,s)]})}}else 3===i?(n.push({op:"move",data:[t[1][0],t[1][1]]}),n.push({op:"bcurveTo",data:[t[1][0],t[1][1],t[2][0],t[2][1],t[2][0],t[2][1]]})):2===i&&n.push(...u(t[0][0],t[0][1],t[1][0],t[1][1],s));return n}function g(t,e,s,i,n,o,h,r){const a=[],u=[],l=c(.5,r)-Math.PI/2;u.push([c(o,r)+e+.9*i*Math.cos(l-t),c(o,r)+s+.9*n*Math.sin(l-t)]);for(let h=l;h<2*Math.PI+l-.01;h+=t){const t=[c(o,r)+e+i*Math.cos(h),c(o,r)+s+n*Math.sin(h)];a.push(t),u.push(t)}return u.push([c(o,r)+e+i*Math.cos(l+2*Math.PI+.5*h),c(o,r)+s+n*Math.sin(l+2*Math.PI+.5*h)]),u.push([c(o,r)+e+.98*i*Math.cos(l+h),c(o,r)+s+.98*n*Math.sin(l+h)]),u.push([c(o,r)+e+.9*i*Math.cos(l+.5*h),c(o,r)+s+.9*n*Math.sin(l+.5*h)]),[u,a]}function d(t,e){return{maxRandomnessOffset:2,roughness:"highlight"===t?3:1.5,bowing:1,stroke:"#000",strokeWidth:1.5,curveTightness:0,curveFitting:.95,curveStepCount:9,fillStyle:"hachure",fillWeight:-1,hachureAngle:-41,hachureGap:-1,dashOffset:-1,dashGap:-1,zigzagOffset:-1,combineNestedSvgPaths:!1,disableMultiStroke:"double"!==t,disableMultiStrokeFill:!1,seed:e}}function p(t,s,n,r,a){const c=[];let u=n.strokeWidth||2;const l=function(t){const e=t.padding;if(e||0===e){if("number"==typeof e)return[e,e,e,e];if(Array.isArray(e)){const t=e;if(t.length)switch(t.length){case 4:return[...t];case 1:return[t[0],t[0],t[0],t[0]];case 2:return[...t,...t];case 3:return[...t,t[1]];default:return[t[0],t[1],t[2],t[3]]}}}return[5,5,5,5]}(n),f=void 0===n.animate||!!n.animate,g=n.iterations||2;switch(n.type){case"underline":{const t=d("single",a),e=s.y+s.h+l[2];for(let n=0;n<g;n++)n%2?c.push(i(s.x+s.w,e,s.x,e,t)):c.push(i(s.x,e,s.x+s.w,e,t));break}case"strike-through":{const t=d("single",a),e=s.y+s.h/2;for(let n=0;n<g;n++)n%2?c.push(i(s.x+s.w,e,s.x,e,t)):c.push(i(s.x,e,s.x+s.w,e,t));break}case"box":{const t=d("single",a),e=s.x-l[3],i=s.y-l[0],n=s.w+(l[1]+l[3]),h=s.h+(l[0]+l[2]);for(let s=0;s<g;s++)c.push(o(e,i,n,h,t));break}case"crossed-off":{const t=d("single",a),e=s.x,n=s.y,o=e+s.w,h=n+s.h;for(let s=0;s<g;s++)s%2?c.push(i(o,h,e,n,t)):c.push(i(e,n,o,h,t));for(let s=0;s<g;s++)s%2?c.push(i(e,h,o,n,t)):c.push(i(o,n,e,h,t));break}case"circle":{const t=d("single",a),e=d("double",a),i=s.w+(l[1]+l[3]),n=s.h+(l[0]+l[2]),o=s.x-l[3]+i/2,r=s.y-l[0]+n/2,u=Math.floor(g/2),f=g-2*u;for(let t=0;t<u;t++)c.push(h(o,r,i,n,e));for(let e=0;e<f;e++)c.push(h(o,r,i,n,t));break}case"highlight":{const t=d("highlight",a);u=.95*s.h;const e=s.y+s.h/2;for(let n=0;n<g;n++)n%2?c.push(i(s.x+s.w,e,s.x,e,t)):c.push(i(s.x,e,s.x+s.w,e,t));break}}if(c.length){const s=function(t){const e=[];for(const s of t){let t="";for(const i of s.ops){const s=i.data;switch(i.op){case"move":t.trim()&&e.push(t.trim()),t=`M${s[0]} ${s[1]} `;break;case"bcurveTo":t+=`C${s[0]} ${s[1]}, ${s[2]} ${s[3]}, ${s[4]} ${s[5]} `;break;case"lineTo":t+=`L${s[0]} ${s[1]} `}}t.trim()&&e.push(t.trim())}return e}(c),i=[],o=[];let h=0;const a=0===n.animationDuration?0:n.animationDuration||800,l=(t,e,s)=>t.setAttribute(e,s);for(const r of s){const s=document.createElementNS(e,"path");if(l(s,"d",r),l(s,"fill","none"),l(s,"stroke",n.color||"currentColor"),l(s,"stroke-width",""+u),f){const t=s.getTotalLength();i.push(t),h+=t}t.appendChild(s),o.push(s)}if(f){let t=0;for(let e=0;e<o.length;e++){const s=o[e],n=i[e],c=h?a*(n/h):0,u=r+t,l=s.style;l.strokeDashoffset=""+n,l.strokeDasharray=""+n,l.animation=`rough-notation-dash ${c}ms ease-out ${u}ms forwards`,t+=c}}}}class _{constructor(t,e){this._state="unattached",this._resizing=!1,this._seed=Math.floor(Math.random()*2**31),this._lastSizes=[],this._animationDelay=0,this._resizeListener=()=>{this._resizing||(this._resizing=!0,setTimeout(()=>{this._resizing=!1,"showing"===this._state&&this.haveRectsChanged()&&this.show()},400))},this._e=t,this._config=JSON.parse(JSON.stringify(e)),this.attach()}get animate(){return this._config.animate}set animate(t){this._config.animate=t}get animationDuration(){return this._config.animationDuration}set animationDuration(t){this._config.animationDuration=t}get iterations(){return this._config.iterations}set iterations(t){this._config.iterations=t}get color(){return this._config.color}set color(t){this._config.color!==t&&(this._config.color=t,this.refresh())}get strokeWidth(){return this._config.strokeWidth}set strokeWidth(t){this._config.strokeWidth!==t&&(this._config.strokeWidth=t,this.refresh())}get padding(){return this._config.padding}set padding(t){this._config.padding!==t&&(this._config.padding=t,this.refresh())}attach(){if("unattached"===this._state&&this._e.parentElement){!function(){if(!window.__rno_kf_s){const t=window.__rno_kf_s=document.createElement("style");t.textContent="@keyframes rough-notation-dash { to { stroke-dashoffset: 0; } }",document.head.appendChild(t)}}();const t=this._svg=document.createElementNS(e,"svg");t.setAttribute("class","rough-annotation");const s=t.style;s.position="absolute",s.top="0",s.left="0",s.overflow="visible",s.pointerEvents="none",s.width="100px",s.height="100px";const i="highlight"===this._config.type;if(this._e.insertAdjacentElement(i?"beforebegin":"afterend",t),this._state="not-showing",i){const t=window.getComputedStyle(this._e).position;(!t||"static"===t)&&(this._e.style.position="relative")}this.attachListeners()}}detachListeners(){window.removeEventListener("resize",this._resizeListener),this._ro&&this._ro.unobserve(this._e)}attachListeners(){this.detachListeners(),window.addEventListener("resize",this._resizeListener,{passive:!0}),!this._ro&&"ResizeObserver"in window&&(this._ro=new window.ResizeObserver(t=>{for(const e of t)e.contentRect&&this._resizeListener()})),this._ro&&this._ro.observe(this._e)}haveRectsChanged(){if(this._lastSizes.length){const t=this.rects();if(t.length!==this._lastSizes.length)return!0;for(let e=0;e<t.length;e++)if(!this.isSameRect(t[e],this._lastSizes[e]))return!0}return!1}isSameRect(t,e){const s=(t,e)=>Math.round(t)===Math.round(e);return s(t.x,e.x)&&s(t.y,e.y)&&s(t.w,e.w)&&s(t.h,e.h)}isShowing(){return"not-showing"!==this._state}refresh(){this.isShowing()&&!this.pendingRefresh&&(this.pendingRefresh=Promise.resolve().then(()=>{this.isShowing()&&this.show(),delete this.pendingRefresh}))}show(){switch(this._state){case"unattached":break;case"showing":this.hide(),this._svg&&this.render(this._svg,!0);break;case"not-showing":this.attach(),this._svg&&this.render(this._svg,!1)}}hide(){if(this._svg)for(;this._svg.lastChild;)this._svg.removeChild(this._svg.lastChild);this._state="not-showing"}remove(){this._svg&&this._svg.parentElement&&this._svg.parentElement.removeChild(this._svg),this._svg=void 0,this._state="unattached",this.detachListeners()}render(t,e){let s=this._config;e&&(s=JSON.parse(JSON.stringify(this._config)),s.animate=!1);const i=this.rects();let n=0;i.forEach(t=>n+=t.w);const o=s.animationDuration||800;let h=0;for(let e=0;e<i.length;e++){const r=i[e];s.animationDuration=o*(r.w/n),p(t,i[e],s,h+this._animationDelay,this._seed),h+=s.animationDuration}this._lastSizes=i,this._state="showing"}rects(){const t=[];if(this._svg)if(this._config.multiline){const e=this._e.getClientRects();for(let s=0;s<e.length;s++)t.push(this.svgRect(this._svg,e[s]))}else t.push(this.svgRect(this._svg,this._e.getBoundingClientRect()));return t}svgRect(t,e){const s=t.getBoundingClientRect(),i=e;return{x:(i.x||i.left)-(s.x||s.left),y:(i.y||i.top)-(s.y||s.top),w:i.width,h:i.height}}}return t.annotate=function(t,e){return new _(t,e)},t.annotationGroup=function(t){let e=0;for(const s of t){const t=s;t._animationDelay=e;e+=0===t.animationDuration?0:t.animationDuration||800}const s=[...t];return{show(){for(const t of s)t.show()},hide(){for(const t of s)t.hide()}}},t}({}); |
@@ -10,3 +10,4 @@ import { SVG_NS, DEFAULT_ANIMATION_DURATION } from './model.js'; | ||
this._seed = randomSeed(); | ||
this._animationGroupDelay = 0; | ||
this._lastSizes = []; | ||
this._animationDelay = 0; | ||
this._resizeListener = () => { | ||
@@ -18,4 +19,3 @@ if (!this._resizing) { | ||
if (this._state === 'showing') { | ||
const newSize = this.size(); | ||
if (newSize && this.hasRectChanged(newSize)) { | ||
if (this.haveRectsChanged()) { | ||
this.show(); | ||
@@ -35,4 +35,2 @@ } | ||
set animationDuration(value) { this._config.animationDuration = value; } | ||
get animationDelay() { return this._config.animationDelay; } | ||
set animationDelay(value) { this._config.animationDelay = value; } | ||
get iterations() { return this._config.iterations; } | ||
@@ -100,10 +98,3 @@ set iterations(value) { this._config.iterations = value; } | ||
for (const entry of entries) { | ||
let trigger = true; | ||
if (entry.contentRect) { | ||
const newRect = this.sizeFor(entry.contentRect); | ||
if (newRect && (!this.hasRectChanged(newRect))) { | ||
trigger = false; | ||
} | ||
} | ||
if (trigger) { | ||
this._resizeListener(); | ||
@@ -118,14 +109,25 @@ } | ||
} | ||
sameInteger(a, b) { | ||
return Math.round(a) === Math.round(b); | ||
} | ||
hasRectChanged(rect) { | ||
if (this._lastSize && rect) { | ||
return !(this.sameInteger(rect.x, this._lastSize.x) && | ||
this.sameInteger(rect.y, this._lastSize.y) && | ||
this.sameInteger(rect.w, this._lastSize.w) && | ||
this.sameInteger(rect.h, this._lastSize.h)); | ||
haveRectsChanged() { | ||
if (this._lastSizes.length) { | ||
const newRects = this.rects(); | ||
if (newRects.length === this._lastSizes.length) { | ||
for (let i = 0; i < newRects.length; i++) { | ||
if (!this.isSameRect(newRects[i], this._lastSizes[i])) { | ||
return true; | ||
} | ||
} | ||
} | ||
else { | ||
return true; | ||
} | ||
} | ||
return true; | ||
return false; | ||
} | ||
isSameRect(rect1, rect2) { | ||
const si = (a, b) => Math.round(a) === Math.round(b); | ||
return (si(rect1.x, rect2.x) && | ||
si(rect1.y, rect2.y) && | ||
si(rect1.w, rect2.w) && | ||
si(rect1.h, rect2.h)); | ||
} | ||
isShowing() { | ||
@@ -179,29 +181,46 @@ return (this._state !== 'not-showing'); | ||
render(svg, ensureNoAnimation) { | ||
const rect = this.size(); | ||
if (rect) { | ||
let config = this._config; | ||
if (ensureNoAnimation) { | ||
config = JSON.parse(JSON.stringify(this._config)); | ||
config.animate = false; | ||
} | ||
renderAnnotation(svg, rect, config, this._animationGroupDelay, this._seed); | ||
this._lastSize = rect; | ||
this._state = 'showing'; | ||
let config = this._config; | ||
if (ensureNoAnimation) { | ||
config = JSON.parse(JSON.stringify(this._config)); | ||
config.animate = false; | ||
} | ||
const rects = this.rects(); | ||
let totalWidth = 0; | ||
rects.forEach((rect) => totalWidth += rect.w); | ||
const totalDuration = (config.animationDuration || DEFAULT_ANIMATION_DURATION); | ||
let delay = 0; | ||
for (let i = 0; i < rects.length; i++) { | ||
const rect = rects[i]; | ||
config.animationDuration = totalDuration * (rect.w / totalWidth); | ||
renderAnnotation(svg, rects[i], config, delay + this._animationDelay, this._seed); | ||
delay += config.animationDuration; | ||
} | ||
this._lastSizes = rects; | ||
this._state = 'showing'; | ||
} | ||
size() { | ||
return this.sizeFor(this._e.getBoundingClientRect()); | ||
} | ||
sizeFor(bounds) { | ||
rects() { | ||
const ret = []; | ||
if (this._svg) { | ||
const rect1 = this._svg.getBoundingClientRect(); | ||
const rect2 = bounds; | ||
const x = (rect2.x || rect2.left) - (rect1.x || rect1.left); | ||
const y = (rect2.y || rect2.top) - (rect1.y || rect1.top); | ||
const w = rect2.width; | ||
const h = rect2.height; | ||
return { x, y, w, h }; | ||
if (this._config.multiline) { | ||
const elementRects = this._e.getClientRects(); | ||
for (let i = 0; i < elementRects.length; i++) { | ||
ret.push(this.svgRect(this._svg, elementRects[i])); | ||
} | ||
} | ||
else { | ||
ret.push(this.svgRect(this._svg, this._e.getBoundingClientRect())); | ||
} | ||
} | ||
return null; | ||
return ret; | ||
} | ||
svgRect(svg, bounds) { | ||
const rect1 = svg.getBoundingClientRect(); | ||
const rect2 = bounds; | ||
return { | ||
x: (rect2.x || rect2.left) - (rect1.x || rect1.left), | ||
y: (rect2.y || rect2.top) - (rect1.y || rect1.top), | ||
w: rect2.width, | ||
h: rect2.height | ||
}; | ||
} | ||
} | ||
@@ -215,3 +234,3 @@ export function annotate(element, config) { | ||
const ai = a; | ||
ai._animationGroupDelay = delay; | ||
ai._animationDelay = delay; | ||
const duration = ai.animationDuration === 0 ? 0 : (ai.animationDuration || DEFAULT_ANIMATION_DURATION); | ||
@@ -218,0 +237,0 @@ delay += duration; |
{ | ||
"name": "rough-notation", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Create and animate hand-drawn annotations on a web page", | ||
@@ -36,2 +36,2 @@ "main": "lib/rough-notation.cjs.js", | ||
} | ||
} | ||
} |
@@ -9,3 +9,3 @@ ![Rough Notation logo](https://roughnotation.com/images/social.png) | ||
Rough Notation is 3.63kb in size when gzipped. | ||
Rough Notation is 3.68kb in size when gzipped. | ||
@@ -99,2 +99,5 @@ [Visit website to see it in action](https://roughnotation.com/) and check out the [source code](https://github.com/pshihn/rough-notation-web) for the website | ||
#### multiline | ||
This property only applies to inline text. To annotate multiline text (each line separately), set this property to `true`. | ||
#### iterations | ||
@@ -151,1 +154,2 @@ By default annotations are drawn in two iterations, e.g. when underlining, drawing from left to right and then back from right to left. Setting this property can let you configure the number of iterations. | ||
- [Web Component Rough Notation](https://github.com/Matsuuu/vanilla-rough-notation) | ||
- [Angular Rough Notation](https://github.com/mikyaj/ngx-rough-notation) |
57804
659
153