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();

}

}

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

  1. html绘制圆形和弧形的代码,html5 canvas用来绘制弧形的代码实现

    这篇文章给大家分享的内容是关于html5 canvas用来绘制弧形的代码实现,有一定的参考价值,有需要的朋友可以从参考一下,希望对你有所帮助. 1.绘制弧形context.arc( centerx, ...

  2. html5拓扑图图入门,如何使用HTML5 Canvas动态的绘制拓扑图

    如何使用HTML5 Canvas动态的绘制拓扑图 使用HTML5 Canvas动态的绘制拓扑图: HTML5中引入新的元素canvas,其drawImage 方法允许在 canvas 中插入其他图像( ...

  3. html 文本框弧形,html5 canvas用来绘制弧形的代码实现

    这篇文章给大家分享的内容是关于html5 canvas用来绘制弧形的代码实现,有一定的参考价值,有需要的朋友可以从参考一下,希望对你有所帮助. 1.绘制弧形context.arc( centerx, ...

  4. html音乐播放器 频谱,HTML5 Canvas 实现简易 绘制音乐环形频谱图

    0.启发 在B站我们有很多的小伙伴们应该都看到过用AE做的可视化音乐播放器播放音乐的视频,看着特别酷炫带感有木有. B站截图 B站截图 B站截图 所以今天我就用 Canvas 做个简单 环形频谱图. ...

  5. 精通Android自定义View(十)绘制篇Canvas分析之绘制Path

    1 Path常用方法简析 Path在2D绘图中是一个很重要的类. Path在这里可以绘制基本的图形,也可以绘制其他复杂的图形. 2 常用API解析与示例 2.1 xxxTo方法 Path类中提供了一套 ...

  6. 精通Android自定义View(九)绘制篇Canvas分析之绘制图片

    绘制图片分为:绘制矢量图(drawPicture)和 绘制位图(drawBitmap) 1 drawBitmap 1.1 基本的绘制图片方法 //Bitmap:图片对象,left:偏移左边的位置,to ...

  7. 制作html动态链接,html5 canvas万花筒动态链接动画

    JavaScript 语言: JaveScriptBabelCoffeeScript 确定 var canvas = document.createElement("canvas" ...

  8. html5 canvas通过javascript绘制漂亮的时钟

    小菜鸟新进博客园,发布一个时钟来纪念一下这个历史性的时刻!介于我比较喜欢那些循循善诱的教学方法,我也将遵循这种方式,一步一步讲述这个过程.让我们开始吧~ 准备工作: 首先新建一个空的Web应用程序,并 ...

  9. html语言填充没有只有描边,HTML5 Canvas笔记——交互绘制文本(描边、填充、阴影、渐变填充、图案填充、文本的属性设置)...

    (1)文本的描边.填充.阴影 (2)文本的渐变填充 (3)文本的图案填充 (4)文本的属性设置及效果呈现 交互绘制文本.html 交互绘制文本 body { background: #eeeeee; ...

最新文章

  1. 香港浸会大学计算机系月入4万博士你还不心动吗?
  2. Yii-mongo操作
  3. 支持字母数字下划线和中文的正则
  4. PHP实现各种经典算法
  5. 【Python】pyCryptodome模块实现AES加密、解密
  6. 妙用Python集合求解啤酒问题(携程2016笔试题)
  7. linkerd mysql_linkerd ab部署测试
  8. Docker学习笔记 之 Docker安装配置使用
  9. SMOTE(Synthetic Minority Over-Sampling Technique ,即“人工少数类过采样法“)----Python调包简单实现
  10. php怎么初始化融云sdk,初始化融云 SDK
  11. GPT分区和MBR分区切换
  12. 读者提问:如何提高效率?
  13. java 跨年 周计算公式_Java关于周跨年的周数计算
  14. 超立方体(n方体)Qn:递推式 和 性质
  15. 数据分析常见的英文缩写(一)
  16. Ubuntu登录界面键盘鼠标失灵
  17. 神经网络与深度学习三:编写单隐层神经网络
  18. 怎么样导包测试JavaWeb代码?
  19. ICMP协议及报文类型含义
  20. linux和xp区别,请教:谁能简单的说一下,linux系统与xp系统的主要区别吗?

热门文章

  1. 信息安全工程师笔记-工控安全需求分析与安全保护工程
  2. MDK解决方案:error: #29: expected an expression
  3. 工单系统中工单的分配及流转
  4. win10系统怎样安装Office2016?win10安装Office2016方法
  5. Java面向对象和MVC模式设计练习——快递管理系统
  6. 第一次写博客,请大家多多包涵...
  7. 小程序警告:Now you can provide attr `wx:key` for a `wx:for` to improve performance.
  8. clustalo安装
  9. margin相对的元素
  10. 联想启天m410进bios_联想启天M410(C)台式机怎么装win7系统