# OPC 拉桿：把數字變成邊跑邊調的控制項

正式網址：https://newmediaart.course.monolab.world/snippets/opc-sliders

OpenProcessing 的函式庫 OP Configurator 3000（OPC）：把 let radius = 10 換成 OPC.slider(...)，畫面旁邊就多一根拉桿，不用改程式、不用重跑就能試數字。拉桿、開關、顏色、下拉選單都有。

- **分類**：起手模板
- **行數**：24 行（p5.js 1.x／2.x）
- **在哪一堂用到**：[第 3 堂](https://newmediaart.course.monolab.world/lesson/03)

在 OpenProcessing 的草圖設定打開 Libraries 的 OP Configurator 3000（沒打開會報 OPC is not defined）。把想試的數字從 let 改成 OPC.slider，畫面旁邊出現拉桿就可以邊跑邊調；調到喜歡的數字記下來。分享時網址加 ?sketch_radius=30 可以換預設值。

```js
// OPC（OP Configurator 3000）：把數字變成拉桿，邊跑邊調。說明：https://github.com/msawired/OPC
// OpenProcessing：草圖設定的 Libraries 打開「OP Configurator 3000」（Fork 講師的粒子模板就已經開好）
// 原本寫 let radius = 10; 的地方換成下面這行；radius 是我取的名字，OPC.slider 是函式庫的指令。不要再 let 一次 radius
OPC.slider('radius', 10, 10, 100); // 名字、預設、最小、最大
OPC.slider('count', 10, 1, 50, 1); // 第五個數字：每格跳多少
OPC.toggle('trail', false); // 開關：true／false
OPC.color('bg', '#646464'); // 顏色

function setup() {
  createCanvas(windowWidth, windowHeight);
  background(bg);
}

function draw() {
  if (!trail) background(bg); // trail 關掉才清畫面
  for (let i = 0; i < count; i++) {
    circle(mouseX + random(-radius, radius), mouseY + random(-radius, radius), radius * 0.3);
  }
}

// 拉桿一動就會叫這個函式（名字是 OPC 規定的）
function parameterChanged(name, value) {
  if (name === 'bg') background(value);
}
```

## 同類常用指令・起手模板

- [粒子起手模板：Hahow Week 3-0 Particle Template](https://newmediaart.course.monolab.world/snippets/particle-template)

[回到全部常用指令](https://newmediaart.course.monolab.world/snippets)

[到 Hahow 報名](https://hahow.in/cr/creative-code)。價格與優惠以 Hahow 頁面為準。
