Added line type

This commit is contained in:
Devine Lu Linvega 2019-07-13 14:28:41 +09:00
parent 44414600a1
commit 1729dc7fa0
4 changed files with 43 additions and 9 deletions

View File

@ -25,7 +25,6 @@ function Commander (ronin) {
this.run = function (txt = this._input.value) { this.run = function (txt = this._input.value) {
console.log('========') console.log('========')
const inter = new Lisp(txt, ronin.library) const inter = new Lisp(txt, ronin.library)
console.log(inter)
inter.toPixels() inter.toPixels()
} }

View File

@ -12,8 +12,12 @@ function Library (ronin) {
return { w, h } return { w, h }
} }
this.rect = (x, y, w, h) => { this.rect = (x, y, w, h, t = 'rect') => {
return { x, y, w, h } return { x, y, w, h, t }
}
this.line = (a, b, t = 'line') => {
return { a, b, t }
} }
this.frame = () => { this.frame = () => {
@ -24,11 +28,12 @@ function Library (ronin) {
this.clone = (a, b) => { this.clone = (a, b) => {
ronin.surface.clone(a, b) ronin.surface.clone(a, b)
return [a, b]
} }
this.stroke = (rect = this.frame(), thickness, color) => { this.stroke = (shape = this.frame(), thickness, color) => {
ronin.surface.stroke(rect, thickness, color) ronin.surface.stroke(shape, thickness, color)
return rect return shape
} }
this.fill = (rect = this.frame(), color) => { this.fill = (rect = this.frame(), color) => {

View File

@ -22,7 +22,17 @@ function Surface (ronin) {
} }
} }
this.stroke = (rect, width, color) => { // Shape
this.stroke = (shape, width, color) => {
if (shape.t === 'rect') {
this.strokeRect(shape, width, color)
} else if (shape.t === 'line') {
this.strokeLine(shape, width, color)
}
}
this.strokeRect = (rect, width, color) => {
this.context.beginPath() this.context.beginPath()
this.context.moveTo(rect.x, rect.y) this.context.moveTo(rect.x, rect.y)
this.context.lineTo(rect.x + rect.w, rect.y) this.context.lineTo(rect.x + rect.w, rect.y)
@ -35,7 +45,25 @@ function Surface (ronin) {
this.context.closePath() this.context.closePath()
} }
this.fill = (rect, color) => { this.strokeLine = function (line, width, color) {
this.context.beginPath()
this.context.moveTo(line.a.x, line.a.y)
this.context.lineTo(line.b.x, line.b.y)
this.context.lineWidth = width
this.context.strokeStyle = color
this.context.stroke()
this.context.closePath()
}
// Fill
this.fill = (shape, color) => {
if (shape.t === 'rect') {
this.fillRect(shape, color)
}
}
this.fillRect = (rect, color) => {
this.context.beginPath() this.context.beginPath()
this.context.moveTo(rect.x, rect.y) this.context.moveTo(rect.x, rect.y)
this.context.lineTo(rect.x + rect.w, rect.y) this.context.lineTo(rect.x + rect.w, rect.y)

View File

@ -4,4 +4,6 @@
(stroke (rect 15 15 120 80) 2 "red") (stroke (rect 15 15 120 80) 2 "red")
(fill (rect 30 30 120 80) "#72dec2") (fill (rect 30 30 120 80) "#72dec2")
(clear (rect 45 45 45 45)) (clear (rect 45 45 45 45))
(clone (rect 0 0 200 200) (rect 100 100 200 200))) (clone (rect 0 0 200 200) (rect 100 100 200 200))
(stroke (line (pos 15 15) (pos 200 200)) 10 "red")
)