Skip to content

Examples

RNO edited this page Jul 22, 2017 · 6 revisions

Basic usage

const plotter = require('xy-plotter')()
const job = plotter.Job('vortex')

const cx = plotter.width / 2
const cy = plotter.height / 2
const margin = 1
const maxRadius = 100
const sides = 100

for (let r = 0; r < maxRadius; r += margin) {
  for (let a = 0; a <= Math.PI*2; a += (Math.PI*2 / sides)) {
    let d = (a < Math.PI) ? Math.PI : 0
    let x = cx + Math.cos(a) * (r + Math.sin(d)*(maxRadius-r) )
    let y = cy + Math.sin(a) * (r + Math.cos(d)*(maxRadius-r) )
    job.move(x, y)
    job.pen_down()
  }
}

// anonymous call to serial
plotter.Serial('/dev/tty.wchusbserial1410').send(job)

Overriding default config

const plotter = require('xy-plotter')({
  width: 380,
  height: 310,
  pen_positions: {
    up: 0,
    down: 80
  },
  decimals: 2
})

console.log(plotter.width, plotter.height)

console.log(plotter.config)
console.log(plotter.defaultConfig)

Drawing from a SVG file

const path = require('path')
const plotter = require('xy-plotter')()

const job = plotter.Job('hello')
job.svg(path.join(__dirname, 'svg', 'hello.svg'))

plotter.Serial('/dev/tty.wchusbserial1410').send(job)

Exporting a job as an image

const path = require('path')
const plotter = require('xy-plotter')()
const job = plotter.Job('broken-circles')

const cx = plotter.width / 2
const cy = plotter.height / 2
const margin = 1
const sides = 100

for (let r = 2; r < 100; r += margin) {
  for (let a = 0; a <= Math.PI*2; a += (Math.PI*2 / sides)) {
    let dx = (a < Math.PI) ? 50 : -20
    let dy = (a > Math.PI) ? 20 : 50
    let dr = (a > Math.PI * 0.25 && a < Math.PI * 1) ? -50 : 0
    let x = cx + dx + Math.cos(a) * Math.max(r + dr , dx)
    let y = cy + dx - dy + Math.sin(a) * Math.max(r + dr, r + dy)
    job.move(x, y)
    job.pen_down()
  }
}

// writing to a png file
const file = plotter.File()
file.export(job, path.join(__dirname, 'broken-circles.png'))

Saving / loading a job

const path = require('path')
const plotter = require('xy-plotter')()
const job = plotter.Job('circles')
const jobPath = path.join(__dirname, 'circles.json')

const cx = plotter.width / 2
const cy = plotter.height / 2
const margin = 1
const maxRadius = 100
const sides = 100

job.setSpeed(0.8)
for (let r = 0; r < maxRadius; r += margin) job.ellipse(cx, cy, r, maxRadius)
for (let r = 0; r < maxRadius; r += margin) job.ellipse(cx, cy, maxRadius, r)

// saving the job
const file = plotter.File()
file.save(job, jobPath)

// loading the job
let loadedJob = file.load(jobPath)
file.export(loadedJob, path.join(__dirname, 'circles.png'))

Handling multiple jobs

const plotter = require('xy-plotter')()

const job1 = plotter.Job('small-rectangle')
job1.rect(10, 10, 10, 10).pen_up()

const job2 = plotter.Job('big-rectangle')
job2.rect(10, 10, 5, 5).pen_up()

const job3 = plotter.Job('circle')
job3.circle(10, 10, 3, 10).pen_up()

const serial = plotter.Serial('/dev/tty.wchusbserial1410', {
  verbose: true,
  progressBar: false,
  disconnectOnJobEnd: false // set to false when queuing jobs (default true)
});

// you can make use of the promises to queue job...
serial
  .send(job1)
  .then(() => serial.send(job2))
  .then(() => serial.send(job3))
  // you'll need to disconnect manually, as serial.disconnectOnJobEnd is false
  .then(() => serial.disconnect())

// ...or use plotter.Serial.sendList([]);
serial
  .sendList([job1, job2, job3])
  .then(() => serial.disconnect())

Getting the stats of a job

const plotter = require('./../index.js')()
const job = plotter.Job('rectangles')

const margin = 1
const startX = plotter.width / 2
const startY = plotter.height / 2 + margin
const x = startX
const y = startY

job.move(startX, startY);
job.pen_down();
for (let i = 0; i < 100; i += margin) {
  x = startX
  x += i
  job.move(x, y)

  y += i * 2
  job.move(x, y)

  x -= i * 2
  job.move(x, y)

  y -= i * 2 + margin
  job.move(x, y)

  y = startY - i
}
job.pen_up();

let stats = plotter.Stats(job)
let duration = stats.duration
console.log(`Some stats before starting "${job.getName()}" :`)
console.log(`the pen will run a total of ${stats.distance} mm,`)
console.log(`in about ${duration.estimation.value} seconds (${duration.estimation.formatted}).`)
console.log(`Let's say between ${duration.min.formatted} and ${duration.max.formatted} to be sure.`)
console.log(`Now Let's draw !\n`)

const serial = plotter.Serial('/dev/tty.wchusbserial1410')
serial.send(job)

Pausing a job

const plotter = require('./../index.js')()
const job = plotter.Job('red-and-blue-rectangle')

// draw a first rectangle
job.rect(50, 50, 20, 20).pen_up()

// wait until user resume the job
job.wait()

// draw a second rectangle with another pen
job.rect(55, 55, 20, 20)

// anonymous call to serial
plotter.Serial('/dev/tty.wchusbserial1410').send(job)

More

See arnaudjuracek/xy-*

Clone this wiki locally