Home | History | Annotate | Download | only in go-demo
      1 /*
      2  * Copyright 2015 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 package main
      8 
      9 /*
     10 	This is a translation of the example code in
     11 	"experimental/c-api-example/skia-c-example.c" to test the
     12 	go-skia package.
     13 */
     14 
     15 import (
     16 	"log"
     17 	"os"
     18 
     19 	skia "skia.googlesource.com/skia/experimental/go-skia"
     20 )
     21 
     22 func main() {
     23 	imgInfo := &skia.ImageInfo{
     24 		Width:     640,
     25 		Height:    480,
     26 		ColorType: skia.GetDefaultColortype(),
     27 		AlphaType: skia.PREMUL_ALPHATYPE,
     28 	}
     29 
     30 	surface, err := skia.NewRasterSurface(imgInfo)
     31 	if err != nil {
     32 		log.Fatalln(err)
     33 	}
     34 
     35 	canvas := surface.Canvas()
     36 
     37 	fillPaint := skia.NewPaint()
     38 	fillPaint.SetColor(0xFF0000FF)
     39 	canvas.DrawPaint(fillPaint)
     40 	fillPaint.SetColor(0xFF00FFFF)
     41 
     42 	rect := skia.NewRect(100, 100, 540, 380)
     43 	canvas.DrawRect(rect, fillPaint)
     44 
     45 	strokePaint := skia.NewPaint()
     46 	strokePaint.SetColor(0xFFFF0000)
     47 	strokePaint.SetAntiAlias(true)
     48 	strokePaint.SetStroke(true)
     49 	strokePaint.SetStrokeWidth(5.0)
     50 
     51 	path := skia.NewPath()
     52 	path.MoveTo(50, 50)
     53 	path.LineTo(590, 50)
     54 	path.CubicTo(-490, 50, 1130, 430, 50, 430)
     55 	path.LineTo(590, 430)
     56 	canvas.DrawPath(path, strokePaint)
     57 
     58 	fillPaint.SetColor(0x8000FF00)
     59 	canvas.DrawOval(skia.NewRect(120, 120, 520, 360), fillPaint)
     60 
     61 	// // Get a skia image from the surface.
     62 	skImg := surface.Image()
     63 
     64 	// Write new image to file if we have one.
     65 	if skImg != nil {
     66 		out, err := os.Create("testimage.png")
     67 		if err != nil {
     68 			log.Fatal(err)
     69 		}
     70 		defer out.Close()
     71 
     72 		if err := skImg.WritePNG(out); err != nil {
     73 			log.Fatalf("Unable to write png: %s", err)
     74 		}
     75 	}
     76 }
     77