畫布的大小(寬度和高度)可以更改,但畫布的樣式大小不能動態更改。如何通過JavaScript動態更改畫布的樣式大小?
canvas = document.getElementById('test_fabric');
ctx = this.canvas.getContext('2d');
canvas.style.width = 1000; // does not work.
canvas.style.height = 260; // does not work.
canvas.width = 100; // works.
canvas.height = 100; // works.
canvas.width和canvas.height都能正常工作,但canvas.style.width和canvas.style.height都不起作用。
在我的情況下,畫布的樣式大小隻能通過畫布的css文件或內聯樣式進行更改。
canvas#test_fabric {
width:400px;
height:400px;
background:gold;
}
或
<div><canvas id="test_fabric" width="200" height="200"
style="width:200px; height:200px"></canvas></div>
注:當內聯樣式存在CSS文件就失效了。
如何通過JavaScript動態更改畫布的樣式大小?
在此先感謝!
對不起jquery的,我的意思是:http://stackoverflow.com/questions/13779429/canvas-inline-height-and-width-屬性覆蓋與CSS樣式是這是 – Gavriel
您是否嘗試過指定CSS長度單位,例如「px」? 'canvas.style.width =「1000px」' – nnnnnn
我嘗試使用「canvas.setAttribute('width','500');」 和「canvas.setAttribute('height','500');」,當定義沒有樣式的canvas標籤時它們不起作用,並且在使用樣式定義canvas標籤時,Chrome報告消息: 「[Report Only] Reflected to應用內聯樣式,因爲它違反了以下內容安全策略指令:「style-src'self'」。'unsafe-inline'關鍵字,一個散列('sha256-8qEYHk4WwsvJKgLquQfLnw2DRoaMh4sWGNj24adrRLQ =')或一個隨機數('nonce-。 ..')需要啓用內聯執行。「 – Calvin