-
Notifications
You must be signed in to change notification settings - Fork 9
/
canvas_editContext.html
165 lines (142 loc) · 7.22 KB
/
canvas_editContext.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
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
<head>
<style>
canvas:hover {
cursor: default;
}
</style>
</head>
(Note: EditContext can be enabled through <b>#enable-experimental-web-platform-features</b> in about://flags)
<br><br>
Click somewhere in the Canvas below, start typing Chinese/Japanese/Korean,<br>
(and change candidate characters) to see how EditContext works with Canvas to handle IME input: <br><br>
To try the "phrase" mode in Japanese IME (on Windows),<br>
1) type "konosushihaoishiidesu" and press "space".<br>
2) use left/right key to move between phrases and press "space" to open the candidate window<br>
3) use up/down key to choose candidate, press "Esc" to close the candidate window or press "Enter" to commit<br><br>
<canvas id="myCanvas" width="400" height="200" style="border:1px solid #000000;">
</canvas>
<script>
let canvas = document.getElementById("myCanvas");
let canvasContext2D = canvas.getContext("2d");
canvasContext2D.font = "30px Arial";
let charWidth = 30; // The width/height of the (Japanese) character is 30px.
let charHeight = 30;
let caretLength = 30;
let editContext = new EditContext();
canvas.editContext = editContext;
let anchorX = 0; // the anchor point of the composition
let anchorY = 0;
let selectionStart = 0;
let selectionEnd = 0;
function renderCaret(x, y) {
canvasContext2D.beginPath();
canvasContext2D.moveTo(x + 2, y + 9);
canvasContext2D.lineTo(x + 2, y - caretLength);
canvasContext2D.strokeStyle = 'green';
canvasContext2D.stroke();
}
function renderUnderlineDecoration(rangeStart, rangeEnd, style, thickness) {
let lineStartX = anchorX + rangeStart * charWidth;
let offsetX = 2; // space between underlines of different phrases
let lineEndX = anchorX + rangeEnd * charWidth - offsetX;
let offsetY = 7; // space between chars and the underline
let lineY = anchorY + offsetY;
let thickWidth = 3;
let thinWidth = 2;
// Japanese IME returns 'Squiggle' while Chinese returns 'Dotted' for active composition.
// For simplicity we draw dotted line for both.
if (style == 'Squiggle' || style == 'Dotted') {
canvasContext2D.setLineDash([1, 1]); // dotted line pattern
canvasContext2D.beginPath();
canvasContext2D.moveTo(lineStartX,lineY);
canvasContext2D.lineTo(lineEndX, lineY);
canvasContext2D.stroke();
canvasContext2D.setLineDash([]); // reset line pattern
} else if (style == 'Solid') { // Draw solid lines for "phrases" in Japenese IME
canvasContext2D.lineWidth = (thickness == 'Thick')? thickWidth : thinWidth;
canvasContext2D.beginPath();
canvasContext2D.moveTo(lineStartX, lineY);
canvasContext2D.lineTo(lineEndX, lineY);
canvasContext2D.stroke();
canvasContext2D.lineWidth = 1; // reset line width.
}
}
function render() {
canvasContext2D.clearRect(0,0, canvas.width, canvas.height);
// rener texts in EditContext's buffer
canvasContext2D.strokeText(editContext.text, anchorX, anchorY);
if (selectionStart == selectionEnd) {
// if collpased selection, render the caret and update bounds for the caret.
let caretIndex = selectionStart;
let caretX = anchorX + caretIndex * charWidth;
let caretY = anchorY;
renderCaret(caretX, caretY);
let selectionBound = computeCharacterBound(caretIndex);
selectionBound.width = 1; // 1px caret
editContext.updateSelectionBounds(selectionBound);
} else {
// TODO: render the selection and update the selection bounds
}
let controlBound = canvas.getBoundingClientRect();
editContext.updateControlBounds(controlBound);
}
// when EditContext is active (i.e. the associated element is focused)
// it'll receive textupdate events when there is user input.
editContext.addEventListener("textupdate", e => {
// The standardized values are e.text, e.selectionStart, and e.selectionEnd.
// The Chromium implementation was recently switched [1] to these values, so for
// now keep handling the old values as that change rolls out.
// [1] https://chromium-review.googlesource.com/c/chromium/src/+/4370629
let text = e.updateText || e.text || '';
let selectionStart = e.newSelectionStart || e.selectionStart;
let selectionEnd = e.newSelectionEnd || e.selectionEnd;
console.log(`editcontext.text:[${editContext.text}]`);
console.log(`textupdate:[${text}], composition range (${e.updateRangeStart}, ${e.updateRangeEnd}), selection (${selectionStart}, ${selectionEnd})`);
selectionStart = e.newSelectionStart;
selectionEnd = e.newSelectionEnd;
render();
});
function computeCharacterBound(offset) {
let margin = 10;
let charX = canvas.offsetLeft + anchorX + offset * charWidth;
let charY = canvas.offsetTop + anchorY;
return DOMRect.fromRect({x:charX, y:charY-charHeight, width:charWidth, height:charHeight+margin});
}
editContext.addEventListener("characterboundsupdate", e => {
console.log(`editcontext.text:[${editContext.text}]`);
console.log(`characterboundsupdate: composition range (${e.rangeStart}, ${e.rangeEnd})`);
let rangeStart = e.rangeStart;
let rangeEnd = e.rangeEnd;
// Calculate bounds for each character in the range.
if (rangeEnd > rangeStart) {
let charBounds = [];
for (offset = rangeStart; offset < rangeEnd; offset++) {
let bound = computeCharacterBound(offset);
charBounds.push(bound);
}
editContext.updateCharacterBounds(rangeStart, charBounds);
console.log("charcterBoundsRangeStart:");
console.log(editContext.characterBoundsRangeStart);
console.log("charcterBounds:");
console.log(editContext.characterBounds());
}
});
function handleTextFormat(textFormat) {
console.log(`textformatupdate: range(${textFormat.rangeStart}, ${textFormat.rangeEnd}),${textFormat.underlineColor}, ${textFormat.backgroundColor}, ${textFormat.textColor},${textFormat.underlineThickness}, ${textFormat.underlineStyle}`);
renderUnderlineDecoration(textFormat.rangeStart, textFormat.rangeEnd, textFormat.underlineStyle, textFormat.underlineThickness);
}
editContext.addEventListener("textformatupdate", e => {
let textFormats = e.getTextFormats();
console.log(`textformatupdate: size(${textFormats.length})`);
textFormats.forEach((textFormat) => {
handleTextFormat(textFormat);
});
});
canvas.addEventListener('click', function(event) {
let elemLeft = canvas.offsetLeft + canvas.clientLeft;
let elemTop = canvas.offsetTop + canvas.clientTop;
anchorX = event.pageX - elemLeft;
anchorY = event.pageY - elemTop;
render();
});
</script>