HTML5canvas万花筒的绘制,HTML5/Canvas万花筒镜像绘制画板应用

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

/*

Canvas kaleidoscope drawing. Supports mouse, touch and pen.

Also supports pressure sensitivity on browsers with Point Events (eg using the Surface/Wacom pen in Microsoft Edge)

*/

var kaleido = true;

var segments = 16;

var strokemultiplier = 5;

var bgcolour = "#FFF";

var canvas = document.getElementById("canvas");

var guide = document.getElementById("guide");

var ctx = canvas.getContext("2d");

var guidectx = guide.getContext("2d");

var width = window.innerWidth;

var height = window.innerHeight;

canvas.width = width;

canvas.height = height;

guide.width = width;

guide.height = height;

var ctxprops = {};

var cursor = {};

var currentpoint;

ctx.lineCap = "round";

if ("PointerEvent" in window) {

canvas.addEventListener("pointerdown", onDown);

} else if ("TouchEvent" in window) {

canvas.addEventListener("touchstart", onDown);

}

canvas.addEventListener("mousedown", onDown);

drawGuide(guidectx);

function drawGuide(ctx) {

ctx.clearRect(0, 0, width, height);

ctx.lineWidth = 1;

ctx.strokeStyle = bgcolour === "#FFF" ? "rgba(0,0,0,0.2)" : "rgba(255,255,255,0.2)";

ctx.lineCap = "round";

ctx.save();

ctx.beginPath();

ctx.translate(width / 2, height / 2);

var r = 360 / segments * Math.PI / 180;

for (var i = 0; i < segments; ++i) {

ctx.rotate(r);

ctx.moveTo(0, 0);

ctx.lineTo(0, Math.max(width, height) * -1);

}

ctx.stroke();

ctx.restore();

}

function onDown(e) {

e.stopPropagation();

e.preventDefault();

if (currentpoint) return;

currentpoint = 1;

if (e.type == "pointerdown") {

currentpoint = e.pointerId;

if (e.button === 5) {

//eraser

ctxprops.globalCompositeOperation = ctx.globalCompositeOperation;

ctxprops.strokeStyle = ctx.strokeStyle;

ctx.globalCompositeOperation = "destination-out";

ctx.strokeStyle = "rgba(0,0,0,1)";

}

document.addEventListener("pointermove", onMove);

document.addEventListener("pointerup", onUp);

} else if (e.type == "touchstart") {

document.addEventListener("touchmove", onMove);

document.addEventListener("touchend", onUp);

} else {

document.addEventListener("mousemove", onMove);

document.addEventListener("mouseup", onUp);

}

if (e.type == "touchstart") {

cursor.x = cursor.lx = e.touches[0].clientX;

cursor.y = cursor.ly = e.touches[0].clientY;

} else {

cursor.x = cursor.lx = e.clientX;

cursor.y = cursor.ly = e.clientY;

}

render();

}

function onMove(e) {

e.stopPropagation();

e.preventDefault();

if (e.type == "pointermove") {

if (currentpoint !== e.pointerId) return;

ctx.lineWidth = e.pressure * strokemultiplier;

} else {

ctx.lineWidth = strokemultiplier;

}

if (e.type == "touchmove") {

cursor.x = e.touches[0].clientX;

cursor.y = e.touches[0].clientY;

} else {

cursor.x = e.clientX;

cursor.y = e.clientY;

}

render();

cursor.lx = cursor.x;

cursor.ly = cursor.y;

}

function onUp(e) {

if (e.type == "pointerup") {

if (e.pointerId !== currentpoint) return;

if (e.button === 5) {

//eraser

ctx.globalCompositeOperation = ctxprops.globalCompositeOperation;

ctx.strokeStyle = ctxprops.strokeStyle;

}

document.removeEventListener("pointermove", onMove);

document.removeEventListener("pointerup", onUp);

} else if (e.type == "touchend") {

document.removeEventListener("touchmove", onMove);

document.removeEventListener("touchend", onUp);

} else {

document.removeEventListener("mousemove", onMove);

document.removeEventListener("mouseup", onUp);

}

currentpoint = null;

}

function render() {

var r = 360 / segments * Math.PI / 180;

for (var i = 0; i < segments; ++i) {

ctx.save();

ctx.translate(width / 2, height / 2);

ctx.rotate(r * i);

if (kaleido) {

if ((segments % 2 === 0) && i > 0 && i % 2 !== 0) {

ctx.scale(1, -1);

if (segments % 4 === 0) {

ctx.rotate((r));

}

}

}

ctx.beginPath();

ctx.moveTo(cursor.lx - width / 2, cursor.ly - height / 2);

ctx.lineTo(cursor.x - width / 2, cursor.y - height / 2);

ctx.stroke();

ctx.restore();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值