如何在Konva.js中實現命令類Command類以支持撤銷和重做功能?
Konva.js中基於命令模式的撤銷重做功能實現
本文介紹如何在Konva.js繪圖應用中,利用命令模式實現撤銷(Ctrl Z)和重做(Ctrl Y)功能。 我們將圖形操作封裝成命令對象,並使用命令棧管理這些操作,從而實現圖形編輯的回退和前進。
首先,定義一個基礎Command
類:
class Command { constructor() { this.states = []; // 用於存儲狀態快照} execute() { throw new Error('execute method must be implemented'); } undo() { throw new Error('undo method must be implemented'); } saveState(state) { this.states.push(state); } restoreState() { return this.states.pop() || null; // 返回上一個狀態,或null } }
接下來,創建一個具體的命令類,例如繪製矩形的命令:
class DrawRectangleCommand extends Command { constructor(stage, layer, rect) { super(); this.stage = stage; this.layer = layer; this.rect = rect; } execute() { this.saveState(this.layer.toJSON()); // 保存當前圖層狀態this.layer.add(this.rect); this.layer.draw(); } undo() { this.rect.destroy(); const prevState = this.restoreState(); if (prevState) { this.layer.destroyChildren(); // 清空圖層this.layer = Konva.Node.create(prevState, this.stage); // 恢復上一個狀態this.stage.add(this.layer); this.layer.draw(); } } }
然後,實現命令管理器:
class CommandManager { constructor() { this.undoStack = []; this.redoStack = []; } executeCommand(command) { command.execute(); this.undoStack.push(command); this.redoStack = []; // 執行新命令後,清空重做棧} undo() { if (this.undoStack.length === 0) return; const command = this.undoStack.pop(); command.undo(); this.redoStack.push(command); } redo() { if (this.redoStack.length === 0) return; const command = this.redoStack.pop(); command.execute(); this.undoStack.push(command); } }
最後,在Konva.js應用中使用:
const stage = new Konva.Stage({ container: 'container', width: window.innerWidth, height: window.innerHeight }); const layer = new Konva.Layer(); stage.add(layer); const commandManager = new CommandManager(); stage.on('click', (e) => { const rect = new Konva.Rect({ x: e.evt.layerX, y: e.evt.layerY, width: 50, height: 50, fill: 'red', draggable: true }); const command = new DrawRectangleCommand(stage, layer, rect); commandManager.executeCommand(command); }); document.addEventListener('keydown', (e) => { if (e.ctrlKey && e.key === 'z') { commandManager.undo(); } else if (e.ctrlKey && e.key === 'y') { commandManager.redo(); } });
這段代碼實現了簡單的矩形繪製和撤銷重做功能。 您可以擴展Command
類來支持其他圖形操作,例如移動、縮放、旋轉等。 記住在每個操作的execute
方法中保存當前狀態,並在undo
方法中恢復之前狀態。 這將確保您的撤銷重做功能能夠正確工作。
以上是如何在Konva.js中實現命令類Command類以支持撤銷和重做功能?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

歐易交易所app支持蘋果手機下載,訪問官網,點擊“蘋果手機”選項,在App Store中獲取並安裝,註冊或登錄後即可進行加密貨幣交易。

比特幣的價格在20,000到30,000美元之間。 1. 比特幣自2009年以來價格波動劇烈,2017年達到近20,000美元,2021年達到近60,000美元。 2. 價格受市場需求、供應量、宏觀經濟環境等因素影響。 3. 通過交易所、移動應用和網站可獲取實時價格。 4. 比特幣價格波動性大,受市場情緒和外部因素驅動。 5. 與傳統金融市場有一定關係,受全球股市、美元強弱等影響。 6. 長期趨勢看漲,但需謹慎評估風險。

全球十大加密貨幣交易平台包括Binance、OKX、Gate.io、Coinbase、Kraken、Huobi Global、Bitfinex、Bittrex、KuCoin和Poloniex,均提供多種交易方式和強大的安全措施。

Binance、OKX、gate.io等十大數字貨幣交易所完善系統、高效多元化交易和嚴密安全措施嚴重推崇。

靠谱的数字货币交易平台推荐:1. OKX,2. Binance,3. Coinbase,4. Kraken,5. Huobi,6. KuCoin,7. Bitfinex,8. Gemini,9. Bitstamp,10. Poloniex,这些平台均以其安全性、用户体验和多样化的功能著称,适合不同层次的用户进行数字货币交易

2025年全球十大加密貨幣交易所包括Binance、OKX、Gate.io、Coinbase、Kraken、Huobi、Bitfinex、KuCoin、Bittrex和Poloniex,均以高交易量和安全性著稱。

MeMebox 2.0通過創新架構和性能突破重新定義了加密資產管理。 1) 它解決了資產孤島、收益衰減和安全與便利悖論三大痛點。 2) 通過智能資產樞紐、動態風險管理和收益增強引擎,提升了跨鏈轉賬速度、平均收益率和安全事件響應速度。 3) 為用戶提供資產可視化、策略自動化和治理一體化,實現了用戶價值重構。 4) 通過生態協同和合規化創新,增強了平台的整體效能。 5) 未來將推出智能合約保險池、預測市場集成和AI驅動資產配置,繼續引領行業發展。

目前排名前十的虛擬幣交易所:1.幣安,2. OKX,3. Gate.io,4。幣庫,5。海妖,6。火幣全球站,7.拜比特,8.庫幣,9.比特幣,10。比特戳。
