golang入門-- 一個2D的圖形庫學習

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

此庫叫gg,源碼在github。

1、擷取源碼並安裝到本地:

      首先要安裝git (傳送門)  :   https://git-scm.com/download/

      然後就可以通過  go get 命令從git獲得源碼並安裝(此處預設大家已安裝go) :    在cmd 進入GOPATH目錄,然後輸入   :  go get  github.com/fogleman/gg

     一會兒之後你就可以看到GOPATH上已經有了gg的源碼,並且順便把gg的依賴第三方庫也下載了。

2、在src\github.com\fogleman\gg\examples 目錄是gg作者寫的例子。這裡選一個出來練練手:

package main    import (      "github.com/fogleman/gg"      "math/rand"  )    func main() {        const W = 1024      const H = 1024      dc := gg.NewContext(W, H) //上下文,含長和寬      dc.SetRGB(0, 0, 0)        //設定當前色      dc.Clear()                //清理一下上下文,下面開始畫畫        for i := 0; i < 1000; i++ { //畫1000 條線,隨機位置,長度,顏色和透明度          x1 := rand.Float64() * W          y1 := rand.Float64() * H          x2 := rand.Float64() * W          y2 := rand.Float64() * H            r := rand.Float64()          g := rand.Float64()          b := rand.Float64()          a := rand.Float64()*0.5 + 0.5          w := rand.Float64()*4 + 1          dc.SetRGBA(r, g, b, a)          dc.SetLineWidth(w)          dc.DrawLine(x1, y1, x2, y2) //畫線          dc.Stroke()                 //沒有這句是不會把線最終畫出來的      }      dc.SavePNG("lines.png") //儲存上下文為一張圖片  }  

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.