-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·178 lines (150 loc) · 3.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const Color = require('color')
const termImg = require('term-img')
const Canvas = require('canvas')
const meow = require('meow')
const isUrl = require('is-url')
const tempFile = require('tempfile')
const nugget = require('nugget')
const Image = Canvas.Image
const canvas = new Canvas(500, 500)
const context = canvas.getContext('2d')
const img = new Image
const cli = meow(`
Usage:
pixelart <path | url>
-w --width output width
-h --height output height
-p --pixel scale pixel level
-v --version pixelart version
`, {
alias: {
w: 'width',
h: 'height',
p: 'pixel',
v: 'version'
}
})
const file = cli.input[0]
let options = {}
if (!file) {
cli.showHelp()
} else if (!isUrl(file)) {
console.error('it look like not a url')
process.exit(1)
} else {
createReader(canvas, cli.flags)
}
function createReader (canvas, opt) {
let targetFile = file
Object.assign(options, {
pixel: opt.pixel || 6,
row: opt.width || 30,
})
img.crossOrigin = 'Anonymous'
img.onload = () => {
context.drawImage(img, 0, 0, options.row, img.height / img.width * options.row)
// Pixel art
let base64Data = drawCanvas(context)
base64Data = base64Data.toDataURL().substr(base64Data.toDataURL().indexOf(',') + 1)
const canvasData = new Buffer(base64Data, 'base64')
termImg(canvasData, {
width: options.row,
fallback: () => {
console.error('Oops!Not supported here!')
process.exit(1)
}
})
}
img.onerror = err => {
throw err
}
if (isUrl(file)) {
let tempPath = tempFile()
const dir = path.dirname(tempPath)
const target = path.basename(tempPath)
nugget(file, {dir, target, quiet: true}, function (err) {
if (err) {
console.error(err.stack)
process.exit(1)
}
readFile(tempPath)
})
} else {
readFile(file)
}
}
function readFile (file) {
fs.readFile(file, (err, squid) => {
if (err) throw err
img.src = squid
})
}
function getColorArray (__context) {
let imageData = __context.getImageData(0, 0, options.row, img.height / img.width * options.row)
let colorArray = []
if (!imageData) {
return colorArray
}
let data = imageData.data
for (let i = 0; i < data.length; i += 4) {
let r = data[i]
let g = data[i + 1]
let b = data[i + 2]
let a = data[i + 3] / 255
let rgba = `rgba(${[r, g, b, a].join(',')})`
if (a === 0) {
colorArray.push('transparent')
} else {
let color = new Color(rgba)
let invert = false
let hue = false
let hueRotate = false
let sat = false
// handle color modification
if (invert === true) {
color = color.negate()
}
if (typeof hue === 'number') {
color = color.hue(hue)
}
if (typeof hueRotate === 'number') {
color = color.rotate(hueRotate)
}
if (typeof sat === 'number') {
color = color.saturate(sat)
}
let hsla = color.hslaString()
colorArray.push(hsla)
}
}
return colorArray
}
/**
* Draw the pixel art on a canvas
* @param {context} canvas's context
* @return {Node} return a canvas with the pixel art version
*/
function drawCanvas (context) {
let _canvas = new Canvas()
let _context = _canvas.getContext('2d')
let colorArray = getColorArray(context)
let y = -1
let pixel = options.pixel
_canvas.height = (img.height / img.width * options.row) * pixel
_canvas.width = options.row * pixel
colorArray.forEach((color, index) => {
let x = index % options.row
if (x === 0) {
++y
}
if (color !== 'transparent') {
_context.fillStyle = color
_context.fillRect(x * pixel, y * pixel, pixel, pixel)
}
})
return _canvas
}