-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
32 lines (27 loc) · 915 Bytes
/
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
const ArrayLikeToString = arg => Array.prototype.toString.call(arg)
const getTextFeature = (text, color) => {
try {
const canvas = document.createElement('canvas')
canvas.width = 1
canvas.height = 1
const ctx = canvas.getContext('2d')
ctx.textBaseline = 'top'
ctx.font = '100px -no-font-family-here-'
ctx.fillStyle = color
ctx.scale(0.01, 0.01)
ctx.fillText(text, 0, 0)
return ctx.getImageData(0, 0, 1, 1).data
} catch (e) {
return false
}
}
const compareFeatures = (feature1, feature2) => {
const feature1Str = ArrayLikeToString(feature1)
const feature2Str = ArrayLikeToString(feature2)
return feature1Str === feature2Str && feature1Str !== '0,0,0,0'
}
export default function (text) {
const feature1 = getTextFeature(text, '#000')
const feature2 = getTextFeature(text, '#fff')
return feature1 && feature2 && compareFeatures(feature1, feature2)
}