made with esnextbin
Created
August 16, 2016 15:07
-
-
Save peteruithoven/e6178ce064ef0c603545de48a79e40e5 to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<style> | |
canvas { | |
border: 1px solid black; | |
} | |
</style> | |
</head> | |
<body> | |
<canvas width="640" height="480" id="canvas"></canvas> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import earcut from 'earcut'; | |
// with duplication | |
var points = [385.2, 180.3, 475.6, 200.4, 475.6, 200.4, 414.3, 269.8, 414.3, 269.8, 423.2, 361.9, 423.2, 361.9, 338.2, 325.2, 338.2, 325.2, 253.3, 361.9, 253.3, 361.9, 262.1, 269.9, 262.1, 269.9, 200.9, 200.4, 200.9, 200.4, 291.2, 180.3, 291.2, 180.3, 338.2, 100.8, 338.2, 100.8, 385.2, 180.3, 289.3, 265.5, 279, 266.6, 279, 266.6, 286.6, 273.3, 286.6, 273.3, 284.4, 283.4, 284.4, 283.4, 293.3, 278.2, 293.3, 278.2, 302.2, 283.4, 302.2, 283.4, 300, 273.3, 300, 273.3, 307.6, 266.6, 307.6, 266.6, 297.3, 265.5, 297.3, 265.5, 293.3, 256, 293.3, 256, 289.3, 265.5, 365.3, 213.2, 355, 216.7, 355, 216.7, 361.1, 225.6, 361.1, 225.6, 361.3, 236.5, 361.3, 236.5, 371.7, 233.4, 371.7, 233.4, 382.2, 236.5, 382.2, 236.5, 382.4, 225.6, 382.4, 225.6, 388.5, 216.7, 388.5, 216.7, 378.3, 213.2, 378.3, 213.2, 371.7, 204.6, 371.7, 204.6, 365.3, 213.2]; | |
var holes = [20, 40]; | |
// without duplication | |
//var points = [385.2, 180.3, 475.6, 200.4, 414.3, 269.8, 423.2, 361.9, 338.2, 325.2, 253.3, 361.9, 262.1, 269.9, 200.9, 200.4, 291.2, 180.3, 338.2, 100.8, 385.2, 180.3, 289.3, 265.5, 279, 266.6, 286.6, 273.3, 284.4, 283.4, 293.3, 278.2, 302.2, 283.4, 300, 273.3, 307.6, 266.6, 297.3, 265.5, 293.3, 256, 289.3, 265.5, 365.3, 213.2, 355, 216.7, 361.1, 225.6, 361.3, 236.5, 371.7, 233.4, 382.2, 236.5, 382.4, 225.6, 388.5, 216.7, 378.3, 213.2, 371.7, 204.6, 365.3, 213.2]; | |
//var holes = [11, 22]; | |
var canvas = document.getElementById('canvas'); | |
var context = canvas.getContext('2d'); | |
context.beginPath(); | |
for (var i = 0; i < points.length; i += 2) { | |
var x = points[i]; | |
var y = points[i + 1]; | |
if (holes.indexOf(i / 2) === -1) { | |
context.lineTo(x, y); | |
} else { | |
context.moveTo(x, y); | |
} | |
} | |
context.lineWidth = 3; | |
context.stroke(); | |
context.beginPath(); | |
var shape = earcut(points, holes); | |
for (var i = 0; i < shape.length; i ++) { | |
var x = points[shape[i] * 2]; | |
var y = points[shape[i] * 2 + 1]; | |
if (i % 3 === 0) { | |
console.log(i); | |
context.moveTo(x, y); | |
} else { | |
context.lineTo(x, y); | |
} | |
} | |
context.strokeStyle = 'red'; | |
context.lineWidth = 2; | |
context.stroke(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"earcut": "2.1.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _earcut = require('earcut'); | |
var _earcut2 = _interopRequireDefault(_earcut); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
// with duplication | |
var points = [385.2, 180.3, 475.6, 200.4, 475.6, 200.4, 414.3, 269.8, 414.3, 269.8, 423.2, 361.9, 423.2, 361.9, 338.2, 325.2, 338.2, 325.2, 253.3, 361.9, 253.3, 361.9, 262.1, 269.9, 262.1, 269.9, 200.9, 200.4, 200.9, 200.4, 291.2, 180.3, 291.2, 180.3, 338.2, 100.8, 338.2, 100.8, 385.2, 180.3, 289.3, 265.5, 279, 266.6, 279, 266.6, 286.6, 273.3, 286.6, 273.3, 284.4, 283.4, 284.4, 283.4, 293.3, 278.2, 293.3, 278.2, 302.2, 283.4, 302.2, 283.4, 300, 273.3, 300, 273.3, 307.6, 266.6, 307.6, 266.6, 297.3, 265.5, 297.3, 265.5, 293.3, 256, 293.3, 256, 289.3, 265.5, 365.3, 213.2, 355, 216.7, 355, 216.7, 361.1, 225.6, 361.1, 225.6, 361.3, 236.5, 361.3, 236.5, 371.7, 233.4, 371.7, 233.4, 382.2, 236.5, 382.2, 236.5, 382.4, 225.6, 382.4, 225.6, 388.5, 216.7, 388.5, 216.7, 378.3, 213.2, 378.3, 213.2, 371.7, 204.6, 371.7, 204.6, 365.3, 213.2]; | |
var holes = [20, 40]; | |
// without duplication | |
//var points = [385.2, 180.3, 475.6, 200.4, 414.3, 269.8, 423.2, 361.9, 338.2, 325.2, 253.3, 361.9, 262.1, 269.9, 200.9, 200.4, 291.2, 180.3, 338.2, 100.8, 385.2, 180.3, 289.3, 265.5, 279, 266.6, 286.6, 273.3, 284.4, 283.4, 293.3, 278.2, 302.2, 283.4, 300, 273.3, 307.6, 266.6, 297.3, 265.5, 293.3, 256, 289.3, 265.5, 365.3, 213.2, 355, 216.7, 361.1, 225.6, 361.3, 236.5, 371.7, 233.4, 382.2, 236.5, 382.4, 225.6, 388.5, 216.7, 378.3, 213.2, 371.7, 204.6, 365.3, 213.2]; | |
//var holes = [11, 22]; | |
var canvas = document.getElementById('canvas'); | |
var context = canvas.getContext('2d'); | |
context.beginPath(); | |
for (var i = 0; i < points.length; i += 2) { | |
var x = points[i]; | |
var y = points[i + 1]; | |
if (holes.indexOf(i / 2) === -1) { | |
context.lineTo(x, y); | |
} else { | |
context.moveTo(x, y); | |
} | |
} | |
context.lineWidth = 3; | |
context.stroke(); | |
context.beginPath(); | |
var shape = (0, _earcut2.default)(points, holes); | |
for (var i = 0; i < shape.length; i++) { | |
var x = points[shape[i] * 2]; | |
var y = points[shape[i] * 2 + 1]; | |
if (i % 3 === 0) { | |
console.log(i); | |
context.moveTo(x, y); | |
} else { | |
context.lineTo(x, y); | |
} | |
} | |
context.strokeStyle = 'red'; | |
context.lineWidth = 2; | |
context.stroke(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment