機械制圖國家標準的繪圖模板_如何使用p5js構建繪圖應用

機械制圖國家標準的繪圖模板

The theme for week #5 of the Weekly Coding Challenge is:

每周編碼挑戰第5周的主題是:

創建繪圖應用程序 (Creating a Drawing Application)

This is the first application that we are building in the #weeklyCodingChallenge program. So far we have built smaller projects, so this is pretty exciting if you ask me! ?

這是我們在#weeklyCodingChallenge程序中構建的第一個應用程序。 到目前為止,我們已經建立了較小的項目,因此如果您問我,這將非常令人興奮! ?

In this article we’ll use p5js, a drawing library, to build a Drawing Application:

在本文中,我們將使用繪圖庫p5js來構建繪圖應用程序 :

Check out the CodePen here:

在此處查看CodePen:

If you want to learn more about p5js and what it does, you can visit their official website. Basically, I am using it because it works very well on top of the browser’s canvas element by providing a clear API.

如果您想了解更多有關p5js及其功能的信息,可以訪問其官方網站 。 基本上,我使用它是因為它通過提供一個清晰的API在瀏覽器的canvas元素上很好地工作。

HTML (The HTML)

As you can notice in the example above, on the left side of the screen we have a .sidebar. We'll put inside it our 'tools' - a color picker, a weight selector and the clear button (trashcan icon):

您可以在上面的示例中注意到,在屏幕的左側,我們有一個.sidebar 。 我們將在其中放入“工具”-一個color選擇器,一個weight選擇器和一個clear按鈕(垃圾桶圖標):

<div class="sidebar"><ul><li><label for="color">Color:</label><input type="color" id="color" /></li><li><label for="weight">Stroke:</label><input type="number" id="weight" min="2" max="200" value="3" /></li><li><button id="clear"><i class="fa fa-trash"></i></button></li></ul>
</div>

CSS (The CSS)

Using CSS we’ll move the .sidebar and everything that’s inside it in the left side. We will style it a little bit to make it look nicer (nothing fancy, basic CSS):

使用CSS,我們將.sidebar及其內部的所有內容移到左側。 我們將對其進行一些樣式設置,使其看起來更好(沒什么花哨的,基本CSS):

.sidebar {background-color: #333;box-shadow: 0px 0px 10px rgba(30, 30, 30, 0.7);color: #fff;position: absolute;left: 0;top: 0;height: 100vh;padding: 5px;z-index: 1000;
}.sidebar ul {display: flex;justify-content: center;align-items: flex-start;flex-direction: column;list-style-type: none;padding: 0;margin: 0;height: 100%;
}.sidebar ul li {padding: 5px 0;
}.sidebar input,
.sidebar button {text-align: center;width: 45px;
}.sidebar li:last-of-type {margin-top: auto;
}.sidebar button {background-color: transparent;border: none;color: #fff;font-size: 20px;
}.sidebar label {display: block;font-size: 12px;margin-bottom: 3px;
}

Now for the important part…

現在重要的是……

JS / P5JS (The JS / P5JS)

As you might have noticed, we haven’t added a canvas element into our HTML since p5js will create it for us.

您可能已經注意到,我們沒有在HTML中添加canvas元素,因為p5js會為我們創建它。

There are two important functions which we’ll use from the p5js library:

我們將在p5js庫中使用兩個重要的函數:

  • setup — is called once when the program starts. It’s used to define initial environment properties such as screen size and background color.

    設置 —在程序啟動時被調用一次。 它用于定義初始環境屬性,例如屏幕尺寸和背景色。

  • draw —is called directly after setup(). The draw() function continuously executes the lines of code contained inside its block.

    draw —在setup()之后直接調用。 draw()函數連續執行其塊內包含的代碼行。

function setup() {// create a canvas which is full width and heightcreateCanvas(window.innerWidth, window.innerHeight);// Add a white background to the canvasbackground(255);
}function draw() {}

Before moving forward, let’s stop for a moment and see what we want to achieve.

在繼續前進之前,讓我們先停下來看看我們要實現的目標。

So, basically, we want to add a mousepressed eventListener to the canvas that will start 'drawing' a shape inside it as long as the mouseIsPressed.

所以,基本上,我們要添加一個mousepressed事件監聽到canvas ,將啟動“繪制”里面的形狀,只要mouseIsPressed

We’ll create an array of points which we’re going to use to create a path (or a shape) using the beginShape and endShape methods to draw this shape inside the canvas. The shape is going to be constructed by connecting a series of vertices (see vertex for more information).

我們將使用BeginShape和endShape方法創建一個點數組,這些點將用于創建path (或形狀)以在畫布內繪制該形狀。 將通過連接一系列頂點來構造形狀(有關更多信息,請參見頂點 )。

As we want this shape to be re-drawn every time, we’ll put this code inside the draw method:

由于我們希望每次都重新繪制此形狀,因此將這段代碼放入draw方法中:

const path = [];function draw() {// disabled filling geometry - p5js functionnoFill();if (mouseIsPressed) {// Store the location of the mouseconst point = {x: mouseX,y: mouseY};path.push(point);}beginShape();path.forEach(point => {// create a vertex at the specified locationvertex(point.x, point.y);});endShape();
}

As you can see, p5js has a mouseIsPressed flag that we can use to detect when the mouse buttons are pressed.

如您所見,p5js有一個mouseIsPressed標志,我們可以用來檢測何時按下鼠標按鈕。

Everything might look good so far, but there is a big issue. Once the mouse button is released and we try to draw another shape, the last point from the previous shape will be connected to the first point of the new shape. This is definitely not what we want, so we need to change our approach a little bit.

到目前為止,一切看起來都不錯,但是有一個問題。 釋放鼠標按鈕并嘗試繪制其他形狀后,先前形狀的最后一個點將連接到新形狀的第一個點。 這絕對不是我們想要的,因此我們需要稍微改變一下方法。

Instead of having one array of points (the path array), we’ll create a pathsarray and we are going to store all the paths inside it. Basically, we’ll have a double array with points. Also, for this, we will need to keep track of the currentPath while the mouse is still pressed. We’ll reset this array once the mouse button is pressed again. Confusing? ? Let’s see the code and I bet that it will become clearer:

除了創建一個點數組(路徑數組)以外,我們將創建一個pathsarray ,并將所有paths存儲在其中。 基本上,我們將有一個帶有點的雙精度數組。 同樣,為此,我們將需要在仍然按下鼠標的同時跟蹤currentPath 。 再次按下鼠標按鈕后,我們將重置此數組。 令人困惑? ? 讓我們看一下代碼,我敢打賭它將變得更加清晰:

const paths = [];
let currentPath = [];function draw() {noFill();if (mouseIsPressed) {const point = {x: mouseX,y: mouseY};// Adding the point to the `currentPath` arraycurrentPath.push(point);}// Looping over all the paths and drawing all the points inside thempaths.forEach(path => {beginShape();path.forEach(point => {stroke(point.color);strokeWeight(point.weight);vertex(point.x, point.y);});endShape();});
}// When the mouse is pressed, this even will fire
function mousePressed() {// Clean up the currentPathcurrentPath = [];// Push the path inside the `paths` arraypaths.push(currentPath);
}

I also added some comments in the code above, make sure you check them out.

我還在上面的代碼中添加了一些注釋,請確保將它們簽出。

The mousePressed function is called once after every time a mouse button is pressed — p5js stuff! ?

每次按下鼠標按鈕時都會調用一次 mousePressed 函數 -p5js的東西! ?

Great! Now we can draw individual shapes in our canvas! ?

大! 現在我們可以在畫布上繪制單個形狀了! ?

The last thing to do is to hook up those buttons that we created in the HTML and use the values that are inside them to style the shape:

最后要做的是連接我們在HTML中創建的按鈕,并使用其中的值來設置形狀的樣式:

const colorInput = document.getElementById('color');
const weight = document.getElementById('weight');
const clear = document.getElementById('clear');function draw() {noFill();if (mouseIsPressed) {const point = {x: mouseX,y: mouseY,// storing the color and weights provided by the inputs for each pointcolor: colorInput.value,weight: weight.value};currentPath.push(point);}paths.forEach(path => {beginShape();path.forEach(point => {// using the color and the weight to style the strokestroke(point.color);strokeWeight(point.weight);vertex(point.x, point.y);});endShape();});
}clear.addEventListener('click', () => {// Remove all the pathspaths.splice(0);// Clear the backgroundbackground(255);
});

And with this, we have finished our little application! Yay! ?

至此,我們完成了我們的小應用程序! 好極了! ?

整個JS代碼 (The entire JS code)

const colorInput = document.getElementById('color');
const weight = document.getElementById('weight');
const clear = document.getElementById('clear');
const paths = [];
let currentPath = [];function setup() {createCanvas(window.innerWidth, window.innerHeight);background(255);
}function draw() {noFill();if (mouseIsPressed) {const point = {x: mouseX,y: mouseY,color: colorInput.value,weight: weight.value};currentPath.push(point);}paths.forEach(path => {beginShape();path.forEach(point => {stroke(point.color);strokeWeight(point.weight);vertex(point.x, point.y);});endShape();});
}function mousePressed() {currentPath = [];paths.push(currentPath);
}clear.addEventListener('click', () => {paths.splice(0);background(255);
});

Also, make sure that you import the p5js file in your html too before importing this js file.

另外,在導入此js文件之前,請確保p5js html中導入p5js文件。

結論 (Conclusion)

I hope that you liked this drawing app that we’ve built. There are a bunch of functionalities that could be added to this app and I challenge you to let your creative mind to come up with new ideas! ?

希望您喜歡我們構建的此繪圖應用程序。 有很多功能可以添加到此應用程序中,我挑戰您讓您的創意思維提出新想法! ?

What if you could save the drawing as an image (.png or .jpg)? ? (you can do this with the p5js library).

如果可以將圖形另存為圖像( .png.jpg )怎么辦? ? (您可以使用p5js庫執行此操作)。

As of now, we are only checking the mouse events. Maybe you could make it work on mobile, too, by figuring out the touch events? The sky is the limit with the amount of functionalities that could be added to this app!

截至目前,我們僅檢查mouse事件。 通過弄清touch事件,也許您也可以使其在移動設備上工作? 天空是可以添加到此應用程序的功能數量的限制!

I’d love to see what you are going to build! Tweet me @florinpop1705 with your creation!

我很想看看你要建立什么! 用您的創作給我@ florinpop1705發推文 !

You might also like one of the other challenges from the Weekly Coding Challenge program. Check them out here.

您可能還會喜歡“每周編碼挑戰”計劃中的其他挑戰之一。 在這里查看它們。

See ya next time! Happy Coding! ?

下次見! 編碼愉快! ?

Originally published at www.florin-pop.com.

最初在www.florin-pop.com上發布。

翻譯自: https://www.freecodecamp.org/news/how-to-build-a-drawing-app-with-p5js-9b8d16e9364a/

機械制圖國家標準的繪圖模板

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391632.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391632.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391632.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

機器學習常用模型:決策樹_fairmodels:讓我們與有偏見的機器學習模型作斗爭

機器學習常用模型:決策樹TL; DR (TL;DR) The R Package fairmodels facilitates bias detection through model visualizations. It implements a few mitigation strategies that could reduce bias. It enables easy to use checks for fairness metrics and comparison betw…

高德地圖如何將比例尺放大到10米?

2019獨角獸企業重金招聘Python工程師標準>>> var map new AMap.Map(container, {resizeEnable: true,expandZoomRange:true,zoom:20,zooms:[3,20],center: [116.397428, 39.90923] }); alert(map.getZoom());http://lbs.amap.com/faq/web/javascript-api/expand-zo…

Android 手把手帶你玩轉自己定義相機

本文已授權微信公眾號《鴻洋》原創首發&#xff0c;轉載請務必注明出處。概述 相機差點兒是每一個APP都要用到的功能&#xff0c;萬一老板讓你定制相機方不方&#xff1f;反正我是有點方。關于相機的兩天奮斗總結免費送給你。Intent intent new Intent(); intent.setAction(M…

如何在JavaScript中克隆數組

JavaScript has many ways to do anything. I’ve written on 10 Ways to Write pipe/compose in JavaScript, and now we’re doing arrays.JavaScript有許多方法可以執行任何操作。 我已經寫了10種用JavaScript編寫管道/組合的方法 &#xff0c;現在我們正在做數組。 1.傳播…

leetcode 227. 基本計算器 II(棧)

給你一個字符串表達式 s &#xff0c;請你實現一個基本計算器來計算并返回它的值。 整數除法僅保留整數部分。 示例 1&#xff1a; 輸入&#xff1a;s “32*2” 輸出&#xff1a;7 解題思路 利用兩個棧&#xff0c;一個記錄操作數&#xff0c;一個記錄操作符&#xff0c;…

100米隊伍,從隊伍后到前_我們的隊伍

100米隊伍,從隊伍后到前The last twelve months have brought us a presidential impeachment trial, the coronavirus pandemic, sweeping racial justice protests triggered by the death of George Floyd, and a critical presidential election. News coverage of these e…

idea使用 git 撤銷commit

2019獨角獸企業重金招聘Python工程師標準>>> 填寫commit的id 就可以取消這一次的commit 轉載于:https://my.oschina.net/u/3559695/blog/1596669

ES6標準入門(第二版)pdf

下載地址&#xff1a;網盤下載 內容簡介 ES6&#xff08;又名 ES2105&#xff09;是 JavaScript 語言的新標準&#xff0c;2015 年 6 月正式發布后&#xff0c;得到了迅速推廣&#xff0c;是目前業界超級活躍的計算機語言。《ES6標準入門&#xff08;第2版&#xff09;》…

hexo博客添加暗色模式_我如何向網站添加暗模式

hexo博客添加暗色模式同一個網站&#xff0c;兩種不同的配色方案 (Same website, two different color schemes) Last year I made it a point to redesign my website from scratch. I wanted something simple and minimalist looking that clearly stated what this was — …

leetcode 331. 驗證二叉樹的前序序列化

序列化二叉樹的一種方法是使用前序遍歷。當我們遇到一個非空節點時&#xff0c;我們可以記錄下這個節點的值。如果它是一個空節點&#xff0c;我們可以使用一個標記值記錄&#xff0c;例如 #。_9_/ \3 2/ \ / \4 1 # 6 / \ / \ / \ # # # # # # 例如&#xff0…

mongodb數據可視化_使用MongoDB實時可視化開放數據

mongodb數據可視化Using Python to connect to Taiwan Government PM2.5 open data API, and schedule to update data in real time to MongoDB — Part 2使用Python連接到臺灣政府PM2.5開放數據API&#xff0c;并計劃將數據實時更新到MongoDB —第2部分 目標 (Goal) This ti…

4.kafka的安裝部署

為了安裝過程對一些參數的理解&#xff0c;我先在這里提一下kafka一些重點概念,topic,broker,producer,consumer,message,partition,依賴于zookeeper, kafka是一種消息隊列,他的服務端是由若干個broker組成的&#xff0c;broker會向zookeeper&#xff0c;producer生成者對應一個…

javascript初學者_針對JavaScript初學者的調試技巧和竅門

javascript初學者by Priyanka Garg由Priyanka Garg My intended audience for this tutorial is beginner programmers. You’ll learn about frustration-free debugging with chrome dev tools.本教程的目標讀者是初學者。 您將學習使用chrome開發工具進行無挫折的調試。 D…

leetcode 705. 設計哈希集合

不使用任何內建的哈希表庫設計一個哈希集合&#xff08;HashSet&#xff09;。 實現 MyHashSet 類&#xff1a; void add(key) 向哈希集合中插入值 key 。 bool contains(key) 返回哈希集合中是否存在這個值 key 。 void remove(key) 將給定值 key 從哈希集合中刪除。如果哈希…

ecshop 前臺個人中心修改側邊欄 和 側邊欄顯示不全 或 導航現實不全

怎么給個人中心側邊欄加項或者減項 在模板文件default/user_menu.lbi 文件里添加或者修改,一般看到頁面都會知道怎么加,怎么刪,這里就不啰嗦了 添加一個欄目以后,這個地址跳的頁面怎么寫 這是最基本的一個包括左側個人信息,頭部導航欄 <!DOCTYPE html PUBLIC "-//W3C//…

leetcode 706. 設計哈希映射

不使用任何內建的哈希表庫設計一個哈希映射&#xff08;HashMap&#xff09;。 實現 MyHashMap 類&#xff1a; MyHashMap() 用空映射初始化對象 void put(int key, int value) 向 HashMap 插入一個鍵值對 (key, value) 。如果 key 已經存在于映射中&#xff0c;則更新其對應…

數據庫語言 數據查詢_使用這種簡單的查詢語言開始查詢數據

數據庫語言 數據查詢Working with data is becoming an increasingly important skill in the modern workplace. 在現代工作場所中&#xff0c;處理數據已成為越來越重要的技能。 Data is no longer the domain of analysts and software engineers. With todays technology,…

面向對象編程思想-觀察者模式

一、引言 相信猿友都大大小小經歷過一些面試&#xff0c;其中有道經典題目&#xff0c;場景是貓咪叫了一聲&#xff0c;老鼠跑了&#xff0c;主人被驚醒&#xff08;設計有擴展性的可加分&#xff09;。對于初學者來說&#xff0c;可能一臉懵逼&#xff0c;這啥跟啥啊是&#x…

typescript 使用_如何使用TypeScript輕松修改Minecraft

typescript 使用by Josh Wulf通過喬什沃爾夫(Josh Wulf) 如何使用TypeScript輕松修改Minecraft (How to modify Minecraft the easy way with TypeScript) Usually, modifying Minecraft requires coding in Java, and a lot of scaffolding. Now you can write and share Min…

Python:在Pandas數據框中查找缺失值

How to find Missing values in a data frame using Python/Pandas如何使用Python / Pandas查找數據框中的缺失值 介紹&#xff1a; (Introduction:) When you start working on any data science project the data you are provided is never clean. One of the most common …