-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
92 lines (89 loc) · 3.38 KB
/
index.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
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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>HTML Canvas</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<textarea id="source"></textarea>
<button id="button">绘制</button>
<div id="scrollText">
</div>
</body>
<script>
var canvasWidth = 800,
canvasHeight = 600,
scrollbarWidth = 20,
scrollbarMinLength = 40;
var canvas = document.createElement('canvas');
canvas.width = canvasWidth;
canvas.height = canvasHeight;
var ctx = canvas.getContext("2d");
var fontSize = 20;
ctx.font = fontSize + "px courier";
ctx.fillStyle = "black";
ctx.lineWidth = 1;
var scrollbarHeight = scrollbarMinLength;
$("#button").click(function() {
var text = $("#source").val();
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
ctx.strokeRect(0, 0, canvasWidth, canvasHeight);
var lines = text.split("\n"),
scrollWidth = canvasWidth,
scrollHeight = 10 + fontSize * lines.length;
for (var i = 0; i < lines.length; i++) {
ctx.fillText(lines[i], 5, 2*fontSize*(i+1)+5);
scrollWidth = Math.max(scrollWidth, ctx.measureText(lines[i]).width + 10);
}
if (scrollHeight > canvasHeight) {
scrollWidth += scrollbarWidth;
ctx.fillStyle = "#000000";
ctx.fillRect(canvasWidth - scrollbarWidth - 1, 1, scrollbarWidth, canvasHeight - 2);
if (scrollHeight - canvasHeight < canvasHeight - scrollbarMinLength) {
scrollbarHeight = 2*canvasHeight - scrollHeight;
}
ctx.fillStyle = "#CCCCCC";
ctx.fillRect(canvasWidth - scrollbarWidth -1+5, 1, scrollbarWidth - 10, scrollbarHeight);
}
});
scrollbarTop = 1;
var isDrag = false, lastY = 0, seekTop = 1, seekLeft = canvasWidth - scrollbarWidth -1+5;
$(canvas).on("mousedown mousemove mouseup", function(e) {
if (e.type != "mousemove") console.log(e, e.pageX, e.pageY);
if (e.type == "mousedown") {
var x = e.offsetX, y = e.offsetY;
//if (x >= seekLeft && x <= seekLeft + scrollbarWidth
// && y >= seekTop && y <= seekTop + scrollbarHeight) {
isDrag = true;
lastY = e.pageY;
//}
} else if (e.type == "mousemove" && isDrag) {
var thisY = e.pageY;
if (thisY != lastY) {
ctx.save();
ctx.fillStyle = "#000000";
ctx.fillRect(canvasWidth - scrollbarWidth - 1, 1, scrollbarWidth, canvasHeight - 2);
ctx.fillStyle = "#CCCCCC";
ctx.fillRect(canvasWidth - scrollbarWidth -1+5, seekTop+thisY-lastY, scrollbarWidth - 10, scrollbarHeight);
//seekTop = 1+thisY-lastY;
ctx.clearRect(5, 5, canvasWidth - 10 - scrollbarWidth, canvasHeight - 10);
var lines = $("#source").val().split("\n");
ctx.translate(0, lastY-thisY);
ctx.fillStyle = "#000000";
for (var ii = 0; ii < lines.length; ii++) {
ctx.fillText(lines[ii], 5, 2*fontSize*(ii+1)+5);
}
ctx.translate(0, thisY-lastY);
ctx.clearRect(1, 1, canvasWidth - scrollbarWidth - 2, 4);
ctx.clearRect(1, canvasHeight-5, canvasWidth - scrollbarWidth - 2, 4);
ctx.clearRect(canvasWidth - scrollbarWidth - 5, 1, 4, canvasHeight - 2);
ctx.restore();
}
} else if (e.type == "mouseup" && isDrag) {
isDrag = false;
}
});
$("#scrollText").append(canvas);
</script>
</html>