《從零開始, 開發一個 Web Office 套件》系列博客目錄
這是一個系列博客, 最終目的是要做一個基於HTML Canvas 的, 類似於微軟 Office 的 Web Office 套件, 包括: 文檔, 表格, 幻燈片... 等等.
對應的Github repo 地址: https://github.com/zhaokang555/canvas-text-editor
2. 富文本編輯器(MVP)
2.15 Mouse hover over text
2.15.1 再議 Bounding box
首先,讓我們來回顧一下CanvasTextEditorChar
的包圍盒:
如上圖,CanvasTextEditorChar
的包圍盒是由:left
, boundingBoxTop
, width
, height
定義的。另外,其top
僅指的是textBaseLine
的縱坐標,跟包圍盒沒有直接的關系。
我們期望的結果是:當鼠標hover在包圍盒上時,產生相應變化。
所以,不能直接讓CanvasTextEditorChar
直接繼承ResponsiveToMouseHover
,因為二者的top
含義完全不同。而是要讓CanvasTextEditorChar
持有它作為自己的包圍盒。
2.15.2 實現
修改CanvasTextEditorChar
:
- 讓其持有一個
boundingBox
對象:- 添加屬性:
boundingBox: ResponsiveToMouseHover
- 在
constructor
中為boundingBox
初始化
- 添加屬性:
- 當修改Char的位置信息時,要同時更新
boundingBox
的位置信息
- 在
render
中調用boundingBox.render()
同時,修改CursorType
:
添加普通文字對應的鼠標樣式:
然后修改CanvasTextEditorParagraph
和CanvasTextEditor
中對應的destructor
:
2.15.3 效果
為了看得更清楚,我們可以加上一些輔助線。修改ResponsiveToMouseHover.render()
:
然后再看下效果:
(未完待續)