代码来源于openprocessing,考虑到国内不是很好访问,我把我找到的比较好的搬运过来!

@

合集1

参考:https://openprocessing.org/sketch/793375

https://github.com/SourceOf0-HTML/processing-p5.js/tree/master

这个可以体验6种笔触,作者介绍如下:

There are multiple pages. Use the "←" "→" buttons below to switch.

The second page explains the UI.

I will explain the sketches I made in the past.

Some variable names have been changed. I wanted to explain in order, so I divided the processing in detail. The UI has been added so the code is getting longer ...

Video of this sketch in action (Twitter)

https://twitter.com/BUN_information/status/1195300719231791104

Gorilla Sun has written an explanation of each part with animations on his blog. Thank you!

https://gorillasun.de/blog/Simulating-brush-strokes-with-Hookes-Law-in-P5JS-and-Processing

其中三种种笔触的代码如下:

笔触4(流速笔触,速写)

原笔触代码地址:https://openprocessing.org/sketch/748836

特点:越快越粗,很适合画速写。

代码

/*
setup=()=>{m=n=x=y=u=v=0,s=0.08,f=0.8;createCanvas(L=500,L);background(C=255)}
draw=()=>{u+=(m-x)*s,v+=(n-y)*s,u*=f,v*=f,a=x,b=y;strokeWeight(abs(u+v)/3+1);line(x+=u,y+=v,a,b)}
mousePressed=()=>{x=m=mouseX,y=n=mouseY}
mouseDragged=()=>{m=mouseX,n=mouseY}
/**/ function setup() {
mX = mY = x = y = ax = ay = 0;
spring = 0.08;
friction = 0.8;
createCanvas( 500, 500 );
background( 255 );
} function draw() {
ax += ( mX - x ) * spring;
ay += ( mY - y ) * spring;
ax *= friction;
ay *= friction; oldX = x;
oldY = y;
x += ax;
y += ay;
strokeWeight( abs( ax + ay ) / 3 + 1 );
line( x, y, oldX, oldY );
} function mousePressed() {
mX = x = mouseX;
mY = y = mouseY;
} function mouseDragged() {
mX = mouseX;
mY = mouseY;
}

笔触5(流速笔触,晕染)

原笔触代码地址:https://openprocessing.org/sketch/754815

特点:越慢越粗,墨水有晕染感,适合写字。

/*
setup=_=>createCanvas(S=500,S),D=10,m=n=x=y=u=v=r=f=0
draw=_=>{R=r;if(mouseIsPressed){m=mouseX;n=mouseY;!f?(f=1,x=m,y=n):0;u+=(m-x)/2;v+=(n-y)/2;u/=2;v/=2;r=25-sqrt(u*u+v*v)*.7;i=D;while(--i)strokeWeight(R+=(r-R)/D),line(x,y,x+=u/D,y+=v/D)}else if(f){u=v=f=0}}
/**/ function setup() {
createCanvas(S=500,S);
distance = 10;
spring = 0.5;
friction = 0.5;
mX = mY = x = y = ax = ay = r = f = 0;
} function draw() {
oldR = r;
if(mouseIsPressed) {
mX = mouseX;
mY = mouseY;
if(!f) {
f = 1;
x = mX;
y = mY;
}
ax += ( mX - x ) * spring;
ay += ( mY - y ) * spring;
ax *= friction;
ay *= friction;
r = 25 - sqrt( ax*ax + ay*ay ) * 0.7; for( i = 0; i < distance; ++i ) {
oldX = x;
oldY = y;
x += ax / distance;
y += ay / distance;
oldR += ( r - oldR ) / distance;
strokeWeight( oldR );
line( x, y, oldX, oldY );
}
} else if(f) {
ax = ay = f = 0;
}
}
/**/

笔触6(流速笔触,毛笔)

原笔触代码地址:https://openprocessing.org/sketch/755877

特点:越慢越粗,有墨水晕染,线条有割裂,适合写毛笔字。

/**
* Added on October 22, 2020
*
* Hi, I'm BUN.
* I've been surprised at how many people have forked over this sketch, more than I expected. Thanks.
*
* Here is a sketch that explains this code with multiple codes.
* Please use it as a reference.
* https://www.openprocessing.org/sketch/793375
*
* Brief explanation.
* This code primarily uses "Hook's Law".
* In other words, it simulates the motion of a spring.
*
* From the mouse position, the position to be drawn is moved like a spring.
* And the width of the line is changed according to the speed of the movement.
*
** Added on May 8, 2021 **
* Gorilla Sun has written an explanation of each part with animations on his blog. Thank you!
* https://gorillasun.de/blog/Simulating-brush-strokes-with-Hooke's-Law-in-P5JS-and-Processing
**/
function setup() {
createCanvas(windowWidth,windowHeight);
distance = 10;
spring = 0.5;
friction = 0.5;
size = 25;
diff = size/8;
x = y = ax = ay = a = r = f = 0;
} function draw() {
oldR = r;
if(mouseIsPressed) {
mX = mouseX;
mY = mouseY;
if(!f) {
f = 1;
x = mX;
y = mY;
}
ax += ( mX - x ) * spring;
ay += ( mY - y ) * spring;
ax *= friction;
ay *= friction;
a += sqrt( ax*ax + ay*ay ) - a;
a *= 0.6;
r = size - a; for( i = 0; i < distance; ++i ) {
oldX = x;
oldY = y;
x += ax / distance;
y += ay / distance;
oldR += ( r - oldR ) / distance;
if(oldR < 1) oldR = 1;
strokeWeight( oldR+diff );
line( x, y, oldX, oldY );
strokeWeight( oldR );
line( x+diff*2, y+diff*2, oldX+diff*2, oldY+diff*2 );
line( x-diff, y-diff, oldX-diff, oldY-diff );
}
} else if(f) {
ax = ay = f = 0;
}
}
/**/

合集2

压感笔触,这个我暂时也没找到合集也没找到太多现成的就只找到一个,剩下的我打算自己写一下。

笔触1(平板压感笔触,不连续圆点)

原笔触代码地址:https://openprocessing.org/sketch/591905

特点: 有压感,支持平板手写笔,可惜是不连续的圆点。

size(600,600);
var p=0;
this.addEventListener('pointermove', function(ev) {
p=(ev.pressure);
}, false);
background(55);
draw = function(){
ellipse(mouseX,mouseY,20*p,20*p);
};

笔触2(平板压感笔触,连续)【自制】

基于前面的笔触自己合的。

var p=0;
this.addEventListener('pointermove', function(ev) {
p=(ev.pressure);
}, false); function setup() {
createCanvas(S=500,S);
distance = 10;
spring = 0.5;
friction = 0.5;
mX = mY = x = y = ax = ay = r = f = 0;
} function draw() {
if(mouseIsPressed) {
mX = mouseX;
mY = mouseY;
if(!f) {
f = 1;
x = mX;
y = mY;
}
ax += ( mX - x ) * spring;
ay += ( mY - y ) * spring;
ax *= friction;
ay *= friction; for( i = 0; i < distance; ++i ) {
oldX = x;
oldY = y;
x += ax / distance;
y += ay / distance;
strokeWeight( p*20 );
line( x, y, oldX, oldY );
}
} else if(f) {
ax = ay = f = 0;
}
}

笔触3(平板压感笔触,连续,修复上一个的压感不连续的毛病)【自制】

已经公开发布,欢迎测试:https://openprocessing.org/sketch/2249898

var p=0;
// this.addEventListener('pointermove', function(ev) {
// p=(ev.pressure);
// }, false); function setup() {
createCanvas(S=500,S);
distance = 10;
spring = 0.5;
friction = 0.5;
size = 150;
pressure = 0;
mX = mY = x = y = ax = ay = r = f = 0;
} function draw() {
oldX = x;
oldY = y;
if(mouseIsPressed) {
oldR = r;
// get pressure 取第一个touches的压力值
for (i = 0; i < touches.length; i++) {
pressure = touches[i].pressure;
break;
}
r = size * pressure; // 使用压力值来调整圆点大小
if (r < 1) r = 1; // 确保圆点有最小大小
mX = mouseX;
mY = mouseY;
if(!f) {
f = 1;
x = mX;
y = mY;
}
ax += ( mX - x ) * spring;
ay += ( mY - y ) * spring;
ax *= friction;
ay *= friction; for( i = 0; i < distance; ++i ) {
oldX = x;
oldY = y;
x += ax / distance;
y += ay / distance;
// 平滑过度压力值
oldR += ( r - oldR ) / distance;
strokeWeight( oldR ); // 设置基于压力的笔触宽度
// 画一个基于压力大小的线
line( x, y, oldX, oldY );
}
} else if(f) {
ax = ay = f = 0;
}
}

【代码】Processing笔触手写板笔刷代码合集(包含流速、毛笔笔触、压感笔触等多种)的更多相关文章

  1. 《Programming WPF》翻译 第7章 3.笔刷和钢笔

    原文:<Programming WPF>翻译 第7章 3.笔刷和钢笔 为了在屏幕上绘制一个图形,WPF需要知道你想要为图形填充什么颜色以及如何绘制它的边框.WPF提供了一些Brush类型支 ...

  2. .Net 2014 Connect() 相关文章合集

    微软在11月中旬的Connect()研讨会中公布了一系列 2015年的发展规划,今天在MSDN Blog上看到了一篇比较全的相关文章合集,这里转录一下,感兴趣的朋友可以看看. Announcement ...

  3. jsfl调整笔刷的笔触和颜色

    今天在用jsfl写脚本以简化对fla资源的处理工作,在画矩形时需要能自动调整笔刷的笔触颜色,填充颜色透明度,查jsfl文档无果,上网查了多番资料写出了可用代码,共享下: var fill = fl.g ...

  4. 计算机视觉与模式识别代码合集第二版two

    Topic Name Reference code Image Segmentation Segmentation by Minimum Code Length AY Yang, J. Wright, ...

  5. 计算机视觉与模式识别代码合集第二版three

    计算机视觉与模式识别代码合集第二版three     Topic Name Reference code Optical Flow Horn and Schunck's Optical Flow   ...

  6. WooCommerce代码合集整理

    本文整理了一些WooCommerce代码合集,方便查阅和使用,更是为了理清思路,提高自己.以下WooCommerce简称WC,代码放在主题的functions.php中即可. 修改首页和分类页面每页产 ...

  7. Javascript 语言精粹 代码片段合集

    Javascript 语言精粹 代码片段合集 标签:Douglas-Crockford Javascript 最佳实践 原文链接 更好的阅读体验 使用一个method 方法定义新方法 Function ...

  8. 关于下拉刷新你是否真的非常理解还是只会搬砖?附 Android 实例子源代码文件下载地址380个合集

    1,推荐几篇非常有用的博文 原创写的真的非常好 主要讲解原理,整体布局三部分组成以及设置padding等等作用, 下拉的具体实现 滑动到底部具体加载以及判断手势事件,再次推荐作者的 详细讲解 建议先看 ...

  9. 天气类API调用的代码示例合集:全国天气预报、实时空气质量数据查询、PM2.5空气质量指数等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 全国天气预报:数据来自国家气象局,可根据地名.经纬度GPS.IP查 ...

  10. 位置信息类API调用的代码示例合集:中国省市区查询、经纬度地址转换、POI检索等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 中国省市区查询:2017最新中国省市区地址 经纬度地址转换:经纬度 ...

随机推荐

  1. Ollama 模型迁移备份工具 ollamab

    背景 ollama 模型和相关配置文件默认都放在 models 文件夹下,想要把指定模型迁移到其他电脑比较麻烦,所以就有了该工具.还有就是模型下载本身就慢,一次下载多台使用减少下载次数.最重要的是公司 ...

  2. 20 分钟高效掌握 cursor

    本身属于在前端小组的一次小分享,这里做个同步分享. 一.pro 权益说明 目前公司购买了 2 个 pro 月付账号,权益包括无限制 tab 补全与 ai 聊天,但每个月只有 500 个快速请求权益,如 ...

  3. ctfshow web入门 文件包含全部wp

    Web78 <?php if(isset($_GET['file'])){ $file = $_GET['file']; include($file); }else{ highlight_fil ...

  4. kubesphere应用系列(一)部署NET8API

    一.准备工作 1.kubesphere 2.harbor 3.net8 二.创建API应用 1.创建api应用 1.1使用命令创建应用 dotnet new webapi -n YourApiAppN ...

  5. MOS管耗散功率的计算

    MOS管的功率,一般是指Maximum Power Dissipation--Pd,最大的耗散功率,具体是指MOS元件的容许损失,可从产品的热阻上求得.当Tc=25度时,通过附加最大容许损耗Pd,则变 ...

  6. Python - [01] 简介

    人生苦短,我用Python 一.Python 是什么 Python是一个高层次的结合了解释型.编译型.互动性和面向对象的脚本语言. Python的设计具有很强的可读性,相比其他语言经常使用英文关键字, ...

  7. 大数据之路Week10_day05 (Redis总结I)

    正文 1.为什么使用redis 分析:博主觉得在项目中使用redis,主要是从两个角度去考虑:性能和并发.当然,redis还具备可以做分布式锁等其他功能,但是如果只是为了分布式锁这些其他功能,完全还有 ...

  8. C++17 Filesystem 实用教程

    点击查看代码 C++17 标准带来了 std::filesystem库, 提供了强大的工具来处理文件路径, 目录以及其他与文件系统相关的操作. 这篇文章适合 C++ 初学者以及希望掌握 C++17 新 ...

  9. 基于C语言实现UDP服务器

    UDP(User Datagram Protocol,用户数据报协议)是一种无连接的传输层协议,适用于对实时性有较高要求的应用场景,如视频流传输.语音通信.在线游戏等.与TCP不同,UDP不保证数据的 ...

  10. Loongson Log

    就看看能坚持多久吧 22/2/2及以前: 参照想象中的p7内容增添部分版CP0.部分中断/异常机制: 改sram接口:查阅文档func_test.sram相关内容:查阅vivado bram IP核相 ...