3.3. 和代碼的區別 Coming From Code - Origami Studio教程


See how Origami differs from programming. 瞅瞅和編程的區別。

Using functions and adding logic to your prototypes is straightforward in Origami, but a little different to what you might expect if you come from a programming background. If you haven't touched code before, feel free to skip forward to Adding Logic.

在 Origami 原型中使用函數和添加邏輯很簡單,但如果以編程的角度出發可能會有跟您預期的有一些不一樣。如果以前沒有接觸過代碼,可以跳這篇直接學習如何添加邏輯

Patches and functions 模塊和函數

The basic building blocks in Origami are called patches. They are similar to functions as far as they take data input(s), perform an action on it, and produce a result.

Origami 中的基本構建塊稱為模塊。
它們和函數能類似,獲取數據輸入,對其執行操作,并產生結果。

function name(input1, input2, input3) { 
  code to be outputted}

The format of a function in code. 代碼中函數的格式。

Origami patches work in the same way; taking a single or multiple input(s), performing an action, and producing an output — albeit using a slightly different format.

Origami 模塊的工作方式和這個一樣,接收單個或多個輸入,執行動作,并產生輸出 - 盡管格式稍微不同。

A function in Patch format in Origami Studio. 模塊的格式。

What makes Origami unique is the suite of pre-made patches that allow you to listen for various types of interaction, create natural animations, manipulate layer properties, and more. Just like functions in code, you can also create your own patches.

Origam 使用的是一套預制的模塊,讓您可以監聽各種類型的交互,創建自然的動畫,控制圖層屬性等等。跟函數在程序中一樣,也可以封裝自定義模塊。

Inputs and outputs 輸入和輸出

We are using an example of converting Fahrenheit to Celsius. Fahrenheit being our Input value, and Celsius being our returned value, or Output.

我們以將華氏度轉換為攝氏度為例,華氏度是輸入值,攝氏度時返回值或輸出值。

In programming languages such as JavaScript, this calculation would look something like this:

在編程語言,如JavaScript中,看著是這樣的:

function fahrenheitToCelsius(fahrenheit) { 
       return (5/9) * (fahrenheit-32);
}
var text = fahrenheitToCelsius(86);

A JavaScript function converting Fahrenheit to Celsius. 將華氏轉換為攝氏度的 JavaScript 函數。

Calculations done in patches are hidden from view by default. The patch simply presents the published Input(s) and Output(s).

默認情況下,模塊的計算公式將被隱藏。只簡單地顯示輸入和輸出值。
板栗:模塊庫里沒有這個模塊,不知道怎么弄出來的。

An Origami patch converting Fahrenheit to Celsius. Origami 模塊將華氏度轉為攝氏度。

Entering this custom-made patch through Patch > Enter Patch Group ?↓ reveals the same calculations as in code.

點擊 Patch > Enter Patch Group Cmd 進入這個自定義模塊內部,顯示與代碼中相同的計算。

Inner workings of the Fahrenheit to Celsius patch. 模塊的內部運算。

One important thing to take note of is the order of calculations. Code usually follows the traditional order of mathematical operations; prioritizing some operators (/
,*) over others (+,-). Origami simply calculates from the order in which the patches are connected.

要注意的一個重要事情是計算的順序。代碼通常遵循傳統的數學運算順序;優先計算乘、除(/*)其次計算加、減(+-` )。Origami 根據模塊的連接順序進行計算。

Multiple outputs 多重輸出

Returning multiple outputs is easy in Origami. This means patches can be used beyond what functions are typically used for. As an example, here is our temperature calculating patch going beyond Fahrenheit to Celsius:

多重輸出在 Origami 中也很簡單。這意味著模塊可以超出通常使用的功能。例如,這里是我們的溫度計算模塊超過華氏攝氏度:

Temperature conversion patch outputting multiple outputs. 溫度轉換模塊輸出多個值。

Values calculated inside the patch simply need to be published as outputs:

補丁內計算的值只需要作為輸出發布:

Inner workings of the Fahrenheit to Multiple patch. 內部工作的多個模塊。

Logic and conditionals 邏輯和條件

Logic in code, called conditionals, is a little different from the way Origami handles logic. Code usually runs linearly from top to bottom, and requires calculations to be done before moving to the next line.

邏輯在代碼中稱為條件,和 Origami 處理邏輯的方式有些不同。代碼通常從上到下線性運行的,需要在移動到下一行之前進行計算。

In Origami's visually-focused Patch Editor, information flows from left to right (including logical operations), depending on what is being interacted with and/or triggered. For that reason, you won't find patches analogous to if, else, else if, or while found in code. Instead, information passed through patches (usually from an interaction) will only continue to flow unless the comparison is false.

在 Origami 的可視化模塊編輯器中,信息從左到右(包括邏輯操作)依賴于與什么進行交互和/或觸發。因此,您不會找到類似于代碼中的if, else, else if, 或 while 之類的模塊。相反,信息通過模塊(一般來自交互)后會繼續流動,除非值為false。

Doing math 數學

Math in Origami is largely the same as arithmetic operators in code. Origami has patches for most standard operators.

數學在 Origami 中大部分和代碼里的算數運算符一樣。Origami 有大多數標準運算符模塊。

3+2
3-2
3*2
3/2

Simple arithmetic in code. 代碼中的簡單算數。

Simple arithmetic in Origami. Origami 中的簡單算數。

Comparing items 比較

Origami is able to take values and output a true or false boolean value, similar to comparison operators in code.

Origami 能夠取值并輸出一個真或假的布爾值,和代碼中的比較運算符一樣。

3>2
3<2
3==2

Simple comparisons in code. 代碼里簡單的比較

Simple comparisons in Origami. Origami 中簡單的比較。

Comparing with logic 與邏輯比較

This is where things start to diverge. Origami has built-in patches which allow for common logic to be done quickly and easily. For example:

這是開始有差異的地方。Origami 有內置的模塊,可以快速、輕松地完成通用邏輯。例如:

3>=2

Comparison in code. 代碼中的比較。

Comparing with logic in Origami. Origami 中與邏輯比較

In code, a logical operator is usually paired with a comparison operator (as shown above). Origami makes it easy to make comparisons with dedicated patches for common logic:

在代碼中,邏輯運算符通常與比較運算符配對(如上所示)。折紙可以使用通用邏輯模塊進行比較:

&&
!
||

Comparison operators in code. 代碼中的比較運算符。

Comparison operators in Origami. Origami 中的比較運算符。

These patches are useful for checking conditionals—similar to if or else. Take an example of checking to see if a calculation is true:

這些模塊可用于檢查條件 - 類似于ifelse。舉個例子檢查計算是否成立:

if (3>2 && (2==3||2<3) && 2!=>3) {
   code to be executed if true
}

Origami takes the information flowing left to right (analogous to 3>2 && (2==3||2<3) && 2!=>3 above) and outputs either true or false, represented by the output on the And patch:

Origami 的信息從左向右流動 (類似于上面的 to 3>2 && (2==3||2<3) && 2!=>3),并輸出 true 或 false,由 And 模塊輸出口表示:

Chaining multiple calculations can become complex and difficult to manage in code. This task becomes intuitive and flexible when built in Origami.

鏈接多個計算在代碼中可能變得復雜,難以管理。這個任務在內置 Origami 時變得直觀靈活。


Related Learn Content 相關教程

4. Adding Logic 添加邏輯
給過渡和流程添加邏輯。

Related Examples 案例

16. Fahrenheit to Celsius
通過溫度轉換了解 Origami 中的邏輯和模塊。

Related Patches 相關模塊

Greater Than 大于Less Than 小于Equals Exactly 完全等于Greater Than or Equal 大于或等于And 和Not 翻轉Or 至少一個+ 加? 減× 乘/(÷) 除


NEXT UP
4. Adding Logic 添加邏輯
給過渡和流程添加邏輯。


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,106評論 6 542
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,441評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,211評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,736評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,475評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,834評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,829評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,009評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,559評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,306評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,516評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,038評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,728評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,132評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,443評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,249評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,484評論 2 379

推薦閱讀更多精彩內容

  • 書籍:《狼圖騰》 背景: 初中時候在一家盜版書店看到過《狼圖騰》的封面,當時就只有一個模糊的影像,后來高中在書店里...
    姓錢的一家人閱讀 375評論 0 0
  • 任何年紀,你都必須做些讓你投入精力的事情。
    kanbuting閱讀 160評論 1 0
  • 本文導讀:有過痛經的女性都知道那種痛苦,有時候痛到臉色發白冒冷汗,有的甚至會痛暈。痛經怎么辦才好?痛經的女性要注意...
    養生綠然本閱讀 217評論 0 0