Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
NateScarlet committed Aug 17, 2019
0 parents commit f051da8
Show file tree
Hide file tree
Showing 28 changed files with 9,028 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
*.js
19 changes: 19 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
root: true,
env: {
browser: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
],
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
},
plugins: ['@typescript-eslint'],
parser: '@typescript-eslint/parser',
};
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.vscode/*
!/.vscode/cspell.json
!/.vscode/extensions.json

node_modules
/dist
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"semi": true,
"trailingComma": "es5"
}
12 changes: 12 additions & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// cSpell Settings
{
// Version of the setting file. Always 0.1
"version": "0.1",
"// language": [
"\n // language - current active spelling language"
],
// words - list of words to be always considered correct
"words": [
"esnext"
]
}
12 changes: 12 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp

// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"dbaeumer.vscode-eslint",
"streetsidesoftware.code-spell-checker"
],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": []
}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SVG Variable width line

Create svg `path` with each point can have variable width.

Can create line with `PointerEvent.pressure`.

```javascript
import * as svgVariableWidthLine from 'svg-variable-width-line';

svgVariableWidthLine.compute({
points: [{ x: 0, y: 0, w: 1 }, { x: 1, y: 0, w: 0 }],
});
// { d: '<Will be path `d` data>' }
```
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['@babel/env'],
};
17 changes: 17 additions & 0 deletions demo/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
svg#canvas {
width: 800px;
height: 600px;
max-width: 100%;
max-height: 100%;
margin: auto;
border: 1px solid black;
}
svg#canvas path {
fill: black;
fill-rule: nonzero;
stroke: red 1px;
}

body {
text-align: center;
}
18 changes: 18 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>SVG Variable Width Line Demo</title>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<svg
id="canvas"
viewBox="0 0 800 600"
xmlns="http://www.w3.org/2000/svg"
stroke="red"
></svg>
<p>Draw in above area with pressure supported device.</p>
<p>Refresh page to clear canvas.</p>
<p>Pressure: <span id="pressure"></span></p>
</body>
</html>
81 changes: 81 additions & 0 deletions demo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as svgVariableWidthLine from '@';

class DrawingHandler {
public el: SVGSVGElement;
public isDrawing = false;
public points: svgVariableWidthLine.Point[] = [];
public target?: SVGPathElement;
public width = 20;
public lastDrawTime?: number;

public constructor(el: SVGSVGElement) {
this.el = el;
}
onPointerdown(e: PointerEvent): void {
e.preventDefault();
this.isDrawing = true;
this.points = [];
const target = document.createElementNS(
'http://www.w3.org/2000/svg',
'path'
);
this.el.appendChild(target);
this.target = target;
}
get mustTarget(): SVGPathElement {
if (!this.target) {
throw new Error('Should has target');
}
return this.target;
}
onPointermove(e: PointerEvent): void {
if (!this.isDrawing) {
return;
}
e.preventDefault();
if (this.lastDrawTime && Date.now() - this.lastDrawTime < 4) {
return;
}
this.lastDrawTime = Date.now();
const p = svgVariableWidthLine.util.translatePoint(this.el, e);
p.w = e.pressure * this.width;
const lastPoint = this.points.slice(-1)[0];
if (
lastPoint &&
svgVariableWidthLine.vector2.length(lastPoint, p) < this.width
) {
return;
}
this.points.push(p);
this.update();
}
onPointerup(e: PointerEvent): void {
e.preventDefault();
this.isDrawing = false;
delete this.target;
}
install(): void {
this.el.addEventListener('pointerdown', this.onPointerdown.bind(this));
this.el.addEventListener('pointermove', this.onPointermove.bind(this));
this.el.addEventListener('pointerup', this.onPointerup.bind(this));
}
update(): void {
const { d } = svgVariableWidthLine.compute(...this.points);
this.mustTarget.setAttribute('d', d);
}
}

((): void => {
const el = document.querySelector('svg#canvas');
if (!(el instanceof SVGSVGElement)) {
throw Error('Missing canvas element');
}
new DrawingHandler(el).install();
el.addEventListener('pointermove', e => {
const el = document.querySelector('#pressure');
if (!el) {
return;
}
el.textContent = e.pressure.toString();
});
})();
4 changes: 4 additions & 0 deletions demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.ts"]
}
1 change: 1 addition & 0 deletions mochapack.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/**/*.spec.ts
Loading

0 comments on commit f051da8

Please sign in to comment.