游詠婕
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 程式設計,第一章,學生版_協作版,互動藝術程式創作入門,Creative Coding --- # 2. 程式框架 - p5.js 開發入門(學生版_協作版) 在這個單元中,我們將會介紹基礎的 p5.js 語法以及線上編輯器 openprocessing 的操作介面,接著做出一個動態筆刷的小練習,最後會提到一些查詢資源的方式以及如何在本機上執行 p5.js 的檔案。 --- * Processing 介紹 * Processing: https://processing.org/overview/ * Processing 的文件: https://processing.org/reference/ --- * p5.js 介紹 * Lauren Lee McCarthy:https://lauren-mccarthy.com/p5-js * p5.js 官網: https://p5js.org/ * p5.js 文件: https://p5js.org/reference/ --- * 其他相關的工具介紹: * https://creativecoding.in/resources/ * 哲宇老師的 Open Processing 帳號: https://www.openprocessing.org/user/139364/#sketches * 創好帳號後,請記得追蹤課程專用帳號:https://www.openprocessing.org/user/231105 --- [註冊網址](https://www.openprocessing.org) ![](https://i.imgur.com/pPkM74R.gif) --- 設定的email會回收到確認信 ![](https://i.imgur.com/USzIsFk.png) --- - 工具的歷史淵源 - processing https://processing.org/ -> js版本 - Processing 官網 https://processing.org/overview/ - https://processing.org/reference/ - https://p5js.org/ - https://p5js.org/reference/ - p5.js 官網 https://p5js.org/ - https://lauren-mccarthy.com/p5-js - p5.js 參考手冊 https://p5js.org/reference/ --- - 編輯程式碼環境 - [Open Processing 註冊](https://openprocessing.org/signin) - [p5 editor 平台](https://editor.p5js.org/) --- ## 範例練習 這是今天筆刷練習的成品,我們將會學到如何創建一個新的畫布、修改顏色、加上簡單的判斷式後做出互動的畫筆。 ![](https://i.imgur.com/KFen9kO.gif) --- ### 筆刷練習 沒有程式經驗的同學也不用擔心,可以先跟著一起做,感受ㄧ下用程式繪圖的樂趣。中間的許多細節在後續的章節中都會陸續提到,也可以自己看看 文件 ,說不定就能迎刃而解,進而享受解決問題的過程喔 😃 --- 產生一個隨著游標走動的方塊 ![](https://i.imgur.com/nWBVZaC.gif) --- ![](https://hackmd.io/_uploads/r1VxSHBJa.gif) ```javascript= function setup() { createCanvas(windowWidth, windowHeight); background("#ffbf69"); } function draw() { fill("#fb8b24");; rect(mouseX,mouseY,30,50) ![](https://hackmd.io/_uploads/r1VxSHBJa.gif) } ``` --- 讓畫的方塊消失,只剩一個,隨著滑鼠移動 ![](https://i.imgur.com/NsVYCKx.gif) --- ```javascript= ``` --- 只會顯示一個球 ![](https://i.imgur.com/ojKBxHj.gif) --- ```javascript= ``` --- 改變圓圈框限顏色與粗細,frameCount:在draw執行過幾次 可以利用此變數,讓越後面的圓變得不一樣的顏色 ![](https://i.imgur.com/0Ku6jOY.gif) --- ```javascript= ``` --- 隨著游標移動的顏色變化,按下滑鼠按鈕會顯示圓圈 ![](https://i.imgur.com/vv4lFAD.gif) --- ```javascript= function setup() { //初始設定畫面所包含的指令 createCanvas(windowWidth, windowHeight); //給我一張跟視窗大小一樣的畫布 //createCanvas(100, 100); //給我寬為100,高為100的視窗畫布 background(100); //背景顏色,只有一個數字值,0為背景黑色,255為背景白色 //background(231,108,247); //RGB設定背景為(231,108,247) //background("#9999CC"); } var t=0; function draw() { //每秒鐘會進來執行60次(預設),每一次都會從頭執行到尾 //print(mouseIsPressed) //當mouseIsPressed為真(true) 代表使用者按下滑鼠。為假(false)未按滑鼠按鈕 if(mouseIsPressed) { //如果條件成立(true),代表按下滑鼠,進來此處執行程式碼 fill(mouseX %256, mouseY%256,0); ellipse(mouseX, mouseY, 40); } else { //如果條件不成立(false),沒有按下滑鼠,進來此處執行程式碼 fill(255, 255,0); //顯示黃色 rect(mouseX-20, mouseY-20, 40); //產生正方形 } } ``` --- ## 語法回顧 * 老師:不用硬背,忘記的話可以去搜尋文件,玩玩看範例 --- ### 基本函式 * 函式 - 能夠執行特定目的或功能的程式碼。偷偷說,其實短短語法背後藏有一大包程式碼,但先別在意 * setup() 與 draw():可理解為「建立繪圖器」及「繪製」。是用 p5 繪圖必備的兩個函式 --- * createCanvas():創建畫布。可以設定長寬 * background():設定背景顏色 * rect():繪製矩形 * ellipse():繪製橢形(寬高一樣時就是圓形) * stroke():設定邊框顏色 --- * strokeWeight():設定邊框粗細 * fill():設定圖形填色 * print():把數值打印在 console 中 --- ### p5.js 內建變數 #### 變數 - 存有某些數值的神秘空間,叫它的名字就可取得內存的數來做點事 * mouseIsPressed:儲存著滑鼠點擊的狀態(點擊時存著 true ; 無點擊時則是 false) * frameCount:儲存當下的畫面禎數(從渲染開始計算) --- #### 程式的流程控制方法 - 關鍵字 if if 這個關鍵字可以用來告訴電腦不同的狀況該怎麼做不同的事 --- if 的基本語法結構 ``` if(滿足這邊的條件 → 這邊吃到 true) { 這邊會有些程式碼 // 做這些程式指令 // } else { 這邊也會有些程式碼 // 若不滿足條件,則做這邊的程式指令 // } ``` --- 範例網址:https://www.openprocessing.org/sketch/858014 --- - 我的第一個程式 - createCanvas - windowWidth - windowHeight --- - p5的程式結構 - setup() - draw() - frameRate --- - 一些程式基礎 - 什麼是註解 // - Debug - print("...") - 如何尋找相關資料或解問題 --- - 備忘錄 (cheatsheet) ![](https://i.imgur.com/bHSXWW8.png) - 參考別人的創作跟Fork --- - 快速鍵 - Ctrl + F 搜尋單字 - Ctrl + D 利用滑鼠選擇搜尋單字的所有內容,做單字變更 - Alt + 左右 可以選取一個完整單字的選取 - Alt + Delete / Backspace 可以一個完整單字的刪除 - Command + 左右 選擇游標所在後面全部 - Command + Delete / Backspace 刪除游標所在後面全部自 - Tab / Shift + Tab - command+enter 執行 Code --- p5的 Codepen Template (更新版)連結: https://codepen.io/frank890417/pen/LYGQoZy 可以在本機執行的打包檔案 [在這邊](https://drive.google.com/file/d/1yALWLlNrV9QLwn1g5JsfAfZ256iiH2r-/view?usp) --- ### 課後問題(1) * setup() 與 draw() 有什麼不一樣,個別執行的時機跟作用是什麼? * 要如何改變畫布的大小呢? * background() 裡面可以使用哪些參數,試著從文件中找找看 --- ### 課後問題(2) * 希望有按下滑鼠的時候,才根據滑鼠的位置改變顏色要怎麼做到呢? * 如果我想要改變先前軌跡變淡的速度,要怎麼做呢? * 從 openprocessing, pinterest, 或是 codepen 之類的平台找幾個覺得有趣的作品,並試著分析ㄧ下他是怎麼做的吧🤩 --- ## 其他範例 * [brush (OpenProcessing)](https://www.openprocessing.org/sketch/704595) * [Frozen brush (OpenProcessing)](https://www.openprocessing.org/sketch/705194) * [Tapering Circles Brush (OpenProcessing)](https://www.openprocessing.org/sketch/649468) * [Rotary Brush (OpenProcessing)](https://www.openprocessing.org/sketch/595598) * [Generative Brushes (OpenProcessing)](https://www.openprocessing.org/sketch/107816/) * [Interactive Automatic Generative Art with Studio Artist](https://www.youtube.com/watch?v=bFAlPKIlDh4) --- 課程素材僅供教學,不提供其他商業用途 --- - 本機環境設定 - VSCode - 下載範例模板包 - https://p5js.org/get-started/ - https://codepen.io/p5js/pen/wreBKy - 加入好用的插件 live server - 開啟dst資料夾裡面的index.html - 在 script.js裡面寫程式 - 設定sublime快速鍵 安裝 - https://marketplace.visualstudio.com/items?itemName=ms-vscode.sublime-keybindings --- {%youtube bFAlPKIlDh4 %} ---

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully