我一直在嘗試使用bezierCurveTo
HTML5使用bezierCurveTo
在下面找到
<canvas id="thisCan" width="0px" height="0px" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
(function() {
var
// Obtain a reference to the canvas element
// using its id.
htmlCanvas = document.getElementById('thisCan'),
// Obtain a graphics context on the
// canvas element for drawing.
ctx = htmlCanvas.getContext('2d');
var width = 0;
var height = 0;
// Start listening to resize events and
// draw canvas.
initialize();
function initialize()
{
// Register an event listener to
// call the resizeCanvas() function each time
// the window is resized.
window.addEventListener('resize', resizeCanvas, false);
// Draw canvas border for the first time.
resizeCanvas();
}
// Display custom canvas.
// In this case it's a blue, 5 pixel border that
// resizes along with the browser window.
function redraw()
{
ctx.beginPath();
ctx.moveTo(width/2, 0);
ctx.bezierCurveTo(150, 119, 186, 121, 66, 185);
ctx.moveTo(width/2 + width * 0.08 , 0);
ctx.bezierCurveTo(344, 119, 344, 121, 504, 185);
ctx.stroke();
}
// Runs each time the DOM window resize event fires.
// Resets the canvas dimensions to match window,
// then draws the new borders accordingly.
function resizeCanvas()
{
var contentElement = document.getElementsByClassName("content-box post")[0]
width = htmlCanvas.width = (contentElement.clientWidth * 0.75)
height = htmlCanvas.height = (contentElement.offsetWidth*0.75 * 0.5);
redraw();
}
})();
</script>
我打算畫許多曲線之間以及代碼繪製gaussin樣功能繪製高斯函數。 但是,如何根據width
和height
變量使參數爲?
我需要使用寬度和高度參數指定控制點,以便它變成窗口大小不變。
有沒有辦法?
對不起,你能展示完整的代碼並描述你想要的嗎? P.S.不要使用全局變量=)只需發送'width'和'height'到'redraw()'和_Use'requestAnimationFrame()',Luke_代替'resize'(更多地介紹https://developer.mozilla.org/en-US/docs/Web/Events/resize) –
@RudolfManusadzhyan,感謝您分享寶貴的知識。今天只有我開始學習javascript,所以我會記住 - no_globals!用'requestAnimationFrame()',我不尋找動畫。我只需要確保在調整窗口大小時我的圖形不會被忽視。所以我認爲resize()更合適,糾正我,如果我錯了。順便說一句,我解決了這個問題 – Adorn
問題:爲什麼?如果要繪製高斯曲線,只需對曲線進行取樣並通過連接點來繪製它(字面上,通過貫穿點陣列和從一點到另一線的點)。使用貝塞爾曲線在這裏沒有太多意義。 –