-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-data-png.html
40 lines (37 loc) · 1.5 KB
/
export-data-png.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>导出数据图片</title>
</head>
<body>
<script src="./js/fututhreeyear-data.js"></script>
<script>
window.onload = function(){
var positionText = 'var yearPosition = '+JSON.stringify(yearPosition);
var diffHeightText = 'var yearDiffHeight = '+JSON.stringify(yearDiffHeight);
var fillLengthText = 'var yearFillLength = '+JSON.stringify(yearFillLength);
var text = positionText+';'+diffHeightText+';'+fillLengthText+';';
//导出逻辑
var pixel = Math.ceil((text.length + 2) / 3); // 1一个像素存3个字节,
var size = Math.ceil(Math.sqrt(pixel));
var canvas = document.createElement('canvas');
canvas.width = canvas.height = size;
var context = canvas.getContext("2d"),
imageData = context.getImageData(0, 0, canvas.width, canvas.height),
pixels = imageData.data;
for(var i = 0, j = 0, l = pixels.length; i < l; i++){
if (i % 4 == 3) { // alpha会影响png还原
pixels[i] = 255;
continue;
}
var code = text.charCodeAt(j++);
if (isNaN(code)) break;
pixels[i] = code;
}
context.putImageData(imageData, 0, 0);
document.querySelector('body').appendChild(canvas);
}
</script>
</body>
</html>