css二次贝塞尔曲线,Canvas 可拖拉的二次贝塞尔曲线

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

/* compact way of setting PI = Math.PI, sin = Math.sin & so on... :D */

Object.getOwnPropertyNames(Math).map(function(p) {

window[p] = Math[p];

});

var c = document.querySelector('.c') /* canvas element */ ,

w /* canvas width */ , h /* canvas height */ ,

ctx = c.getContext('2d') /* get canvas context */ ,

/* start, end & control points for our quadratic Bézier curve */

start, end, control;

/* FUNCTIONS */

var initCanvas = function() {

/* canvas element stles that were set via CSS */

var s = getComputedStyle(c);

/*

* get canvas dimensions as they were set via CSS

* use them to set w & h variables and

* canvas width & height attributes

* (don't forget about these,

* canvas looks stretched if you do)

*/

w = c.width = ~~s.width.split('px')[0];

h = c.height = ~~s.height.split('px')[0];

/*

* pick initial start, end and control points

* for our quadratic Bézier curve

*/

start = {

'x': round(w / 4),

'y': round(h / 2)

};

end = {

'x': round(3 * w / 4),

'y': round(h / 2)

};

control = {

'x': round(w / 2),

'y': round(h / 2)

};

/*

* make sure the entire canvas is cleared

* before anything else

* this has no visual effect when init is called

* for the first time

*/

ctx.clearRect(0, 0, w, h);

/* set stroke and fill styles */

ctx.strokeStyle = '#fff';

/* note that the fill is semitransparent, alpha = .2 */

ctx.fillStyle = 'hsla(200, 25%, 7%, .2)';

/* call function that handles drawing stuff */

drawOnCanvas();

};

var drawOnCanvas = function() {

/*

* don't clear canvas before drawing omething new

* just cover it with a semitransparent rectangle

* this leaves what was drawn by previous calls

* visible, but somehow "faded",

* creating the "trace effect"

*/

ctx.rect(0, 0, w, h);

ctx.fill();

/*

* draw our quadratic Bézier curve

* for the current control point

*/

ctx.beginPath();

ctx.moveTo(start.x, start.y);

ctx.quadraticCurveTo(control.x, control.y, end.x, end.y);

ctx.fill(); /* for inside the curve */

ctx.stroke();

};

/* START IT ALL */

/*

* inside the setTimeout so that

* the dimensions do get set via CSS before calling

* the init function

*/

setTimeout(function() {

initCanvas();

c.addEventListener('mousemove', function(e) {

/*

* coordinates of the control point

* of a quadratic Bézier curve whose midpoint

* is at the current mouse position

*/

control = {

'x': round(2 * e.clientX - (start.x + end.x) / 2),

'y': round(2 * e.clientY - (start.y + end.y) / 2)

};

/* draw this quadratic Bézier curve */

drawOnCanvas();

}, false);

/* fix looks on resize so it doesn't look stretched */

addEventListener('resize', initCanvas, false);

}, 15);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值