Migrating wiki contents from Google Code
This commit is contained in:
commit
a63d721fb1
10 changed files with 963 additions and 0 deletions
11
.project
Normal file
11
.project
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>draw2d-wiki</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
</natures>
|
||||
</projectDescription>
|
54
GettingStarted.md
Normal file
54
GettingStarted.md
Normal file
|
@ -0,0 +1,54 @@
|
|||
# Getting Started #
|
||||
see http://code.google.com/p/draw2d/source/browse/cmd/gettingStarted.go
|
||||
|
||||
Writing to a png file
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"code.google.com/p/draw2d/draw2d"
|
||||
"image"
|
||||
"image/png"
|
||||
)
|
||||
|
||||
func saveToPngFile(filePath string, m image.Image) {
|
||||
f, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer f.Close()
|
||||
b := bufio.NewWriter(f)
|
||||
err = png.Encode(b, m)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
err = b.Flush()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Wrote %s OK.\n", filePath)
|
||||
}
|
||||
|
||||
func main() {
|
||||
i := image.NewRGBA(image.Rect(0, 0, 200, 200))
|
||||
gc := draw2d.NewGraphicContext(i)
|
||||
gc.MoveTo(10.0, 10.0)
|
||||
gc.LineTo(100.0, 10.0)
|
||||
gc.Stroke()
|
||||
saveToPngFile("TestPath.png", i)
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
# Where to go next #
|
||||
Now you are ready to start using the draw2d package. Go to [Samples](Samples.md) or [GettingStartedGUI](GettingStartedGUI.md).
|
67
GettingStartedGUI.md
Normal file
67
GettingStartedGUI.md
Normal file
|
@ -0,0 +1,67 @@
|
|||
This code implements a basic drawing GUI application. Move you mouse clicking on the window and draw.
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"exp/draw"
|
||||
"exp/draw/x11"
|
||||
"image"
|
||||
"math"
|
||||
"draw2d.googlecode.com/hg/draw2d"
|
||||
)
|
||||
|
||||
func main() {
|
||||
window, err := x11.NewWindow()
|
||||
if(err != nil) {
|
||||
fmt.Printf("Cannot open an x11 window\n")
|
||||
return
|
||||
}
|
||||
screen := window.Screen()
|
||||
if rgba, ok := screen.(*image.RGBA); ok {
|
||||
gc := draw2d.NewGraphicContext(rgba)
|
||||
gc.SetStrokeColor(image.Black)
|
||||
gc.SetFillColor(image.White)
|
||||
gc.Clear()
|
||||
for i := 0.0 ; i < 360; i = i + 10 {// Go from 0 to 360 degrees in 10 degree steps
|
||||
gc.BeginPath() // Start a new path
|
||||
gc.Save() // Keep rotations temporary
|
||||
gc.MoveTo(144, 144)
|
||||
gc.Rotate(i * (math.Pi / 180.0)) // Rotate by degrees on stack from 'for'
|
||||
gc.RLineTo(72, 0)
|
||||
gc.Stroke()
|
||||
gc.Restore() // Get back the unrotated state
|
||||
}
|
||||
fmt.Printf("This is an rgba image\n")
|
||||
|
||||
window.FlushImage()
|
||||
|
||||
gc.SetLineWidth(3)
|
||||
nbclick := 0
|
||||
for {
|
||||
|
||||
switch evt := (<-window.EventChan()).(type) {
|
||||
case draw.KeyEvent:
|
||||
if(evt.Key == 'q') {
|
||||
window.Close()
|
||||
}
|
||||
case draw.MouseEvent:
|
||||
if(evt.Buttons & 1 != 0) {
|
||||
if(nbclick % 2 == 0) {
|
||||
gc.MoveTo(float(evt.Loc.X),float(evt.Loc.Y))
|
||||
} else {
|
||||
gc.LineTo(float(evt.Loc.X),float(evt.Loc.Y))
|
||||
gc.Stroke()
|
||||
window.FlushImage()
|
||||
}
|
||||
nbclick = nbclick + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Not an RGBA image!\n")
|
||||
}
|
||||
}
|
||||
|
||||
```
|
34
Gotour.md
Normal file
34
Gotour.md
Normal file
|
@ -0,0 +1,34 @@
|
|||
# How to use draw2d in gotour #
|
||||
|
||||
|
||||
Install the gotour program locally, install draw2d and run the gotour executable:
|
||||
```
|
||||
go get code.google.com/p/go-tour/gotour
|
||||
go get code.google.com/p/draw2d/draw2d
|
||||
gotour
|
||||
```
|
||||
|
||||
now open the gotour into your browser http://127.0.0.1:3999 and run this snippet of code:
|
||||
|
||||
```
|
||||
package main
|
||||
|
||||
import (
|
||||
"code.google.com/p/draw2d/draw2d"
|
||||
"image"
|
||||
"image/color"
|
||||
"code.google.com/p/go-tour/pic"
|
||||
)
|
||||
|
||||
func main() {
|
||||
i := image.NewRGBA(image.Rect(0, 0, 200, 200))
|
||||
gc := draw2d.NewGraphicContext(i)
|
||||
gc.Save()
|
||||
gc.SetStrokeColor(color.Black)
|
||||
gc.SetFillColor(color.White)
|
||||
draw2d.Rect(gc, 10, 10, 100, 100)
|
||||
gc.FillStroke()
|
||||
gc.Restore()
|
||||
pic.ShowImage(i)
|
||||
}
|
||||
```
|
0
Index.md
Normal file
0
Index.md
Normal file
65
Postscript.md
Normal file
65
Postscript.md
Normal file
|
@ -0,0 +1,65 @@
|
|||
|
||||
```
|
||||
|
||||
package main
|
||||
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"log"
|
||||
"os"
|
||||
"io/ioutil"
|
||||
"bufio"
|
||||
"strings"
|
||||
"image"
|
||||
"image/png"
|
||||
"draw2d.googlecode.com/hg/draw2d"
|
||||
"draw2d.googlecode.com/hg/postscript"
|
||||
)
|
||||
|
||||
|
||||
func saveToPngFile(filePath string, m image.Image) {
|
||||
f, err := os.Open(filePath, os.O_CREAT|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer f.Close()
|
||||
b := bufio.NewWriter(f)
|
||||
err = png.Encode(b, m)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
err = b.Flush()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Wrote %s OK.\n", filePath)
|
||||
}
|
||||
|
||||
func main() {
|
||||
i := image.NewRGBA(600, 800)
|
||||
gc := draw2d.NewGraphicContext(i)
|
||||
gc.Translate(0, 380)
|
||||
gc.Scale(1, -1)
|
||||
gc.Translate(0, -380)
|
||||
lastTime := time.Nanoseconds()
|
||||
src, err := os.Open("../resource/postscript/tiger.ps", 0, 0)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer src.Close()
|
||||
bytes, err := ioutil.ReadAll(src)
|
||||
reader := strings.NewReader(string(bytes))
|
||||
interpreter := postscript.NewInterpreter(gc)
|
||||
interpreter.Execute(reader)
|
||||
dt := time.Nanoseconds() - lastTime
|
||||
fmt.Printf("Draw image: %f ms\n", float64(dt)*1e-6)
|
||||
saveToPngFile("../resource/result/TestPostscript.png", i)
|
||||
}
|
||||
```
|
||||
|
||||

|
45
ProjectHome.md
Normal file
45
ProjectHome.md
Normal file
|
@ -0,0 +1,45 @@
|
|||
This package (written in **[go](http://golang.org)**) provide an API to draw 2d geometrical form on [images](http://golang.org/pkg/image/). This library is largely inspired by [postscript](http://www.tailrecursive.org/postscript/), [cairo](http://cairographics.org/), [HTML5 canvas](http://dev.w3.org/html5/canvas-api/canvas-2d-api.html#the-2d-drawing-context).
|
||||
|
||||
<wiki:gadget url="http://www.ohloh.net/p/488118/widgets/project\_users\_logo.xml" height="43" border="0"/>
|
||||
|
||||
The package depends on [freetype-go](http://code.google.com/p/freetype-go/) package thanks to its rasterization algorithm.
|
||||
|
||||
Some algorithm have been translated from http://www.antigrain.com project ([adaptive bezier](http://www.antigrain.com/research/adaptive_bezier/index.html), and arc drawing)
|
||||
|
||||
Some [News](http://code.google.com/p/draw2d/wiki/news) about the project
|
||||
|
||||
### Installation ###
|
||||
Once you have Go installed:
|
||||
|
||||
* go get code.google.com/p/draw2d/draw2d
|
||||
To use the postscript interpreter
|
||||
* go get code.google.com/p/draw2d/postscript
|
||||
To use the OpenGL port (not tested since last release, may use gogl)
|
||||
* go get code.google.com/p/draw2d/draw2dgl
|
||||
|
||||
a good starting point is the [getting started](http://code.google.com/p/draw2d/wiki/GettingStarted)
|
||||
|
||||
|
||||
### [Samples](http://code.google.com/p/draw2d/wiki/Samples) ###
|
||||
Sample images generated by draw2d (inspired by [cairo samples](http://cairographics.org/samples/)):
|
||||
there's already some bugs please refer to [issue tracking](http://code.google.com/p/draw2d/issues/list)
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
606
Samples.md
Normal file
606
Samples.md
Normal file
|
@ -0,0 +1,606 @@
|
|||
# Test results #
|
||||
|
||||
You can find samples here generated by these program:
|
||||
> http://code.google.com/p/draw2d/source/browse/cmd/testdraw2d.go
|
||||
Those samples are largely inspired by this page:
|
||||
> http://cairographics.org/samples/
|
||||
|
||||
Please see the corresponding HTML canvas, Postscript and svg results from [ajstarks](http://groups.google.com/group/golang-nuts/browse_thread/thread/62288215d63e263a/6d1159b2f965289c#6d1159b2f965289c) (for comparison purpose):
|
||||
* http://draw2d.googlecode.com/hg/resource/image/Tests.html
|
||||
* http://draw2d.googlecode.com/hg/resource/image/test.js
|
||||
* http://draw2d.googlecode.com/hg/resource/image/Tests.ps
|
||||
* http://www.flickr.com/photos/ajstarks/5207943227/
|
||||
|
||||
There's already some bugs please refer to [issue tracking](http://code.google.com/p/draw2d/issues/list)
|
||||
|
||||
|
||||
|
||||
## Test Path ##
|
||||

|
||||
```
|
||||
func TestPath() {
|
||||
i, gc := initGc(w, h)
|
||||
gc.MoveTo(10.0, 10.0)
|
||||
gc.LineTo(100.0, 10.0)
|
||||
gc.LineTo(100.0, 100.0)
|
||||
gc.LineTo(10.0, 100.0)
|
||||
gc.LineTo(10.0, 10.0)
|
||||
gc.FillStroke()
|
||||
saveToPngFile("TestPath", i)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Test Draw Arc ##
|
||||

|
||||
```
|
||||
func TestDrawArc() {
|
||||
i, gc := initGc(w, h)
|
||||
// draw an arc
|
||||
xc, yc := 128.0, 128.0
|
||||
radiusX, radiusY := 100.0, 100.0
|
||||
startAngle := 45.0 * (math.Pi / 180.0) /* angles are specified */
|
||||
angle := 135 * (math.Pi / 180.0) /* in radians */
|
||||
gc.SetLineWidth(10)
|
||||
gc.SetLineCap(draw2d.ButtCap)
|
||||
gc.SetStrokeColor(image.Black)
|
||||
gc.ArcTo(xc, yc, radiusX, radiusY, startAngle, angle)
|
||||
gc.Stroke()
|
||||
// fill a circle
|
||||
gc.SetStrokeColor(image.RGBAColor{255, 0x33, 0x33, 0x80})
|
||||
gc.SetFillColor(image.RGBAColor{255, 0x33, 0x33, 0x80})
|
||||
gc.SetLineWidth(6)
|
||||
|
||||
gc.MoveTo(xc, yc)
|
||||
gc.LineTo(xc+cos(startAngle)*radiusX, yc+sin(startAngle)*radiusY)
|
||||
gc.MoveTo(xc, yc)
|
||||
gc.LineTo(xc-radiusX, yc)
|
||||
gc.Stroke()
|
||||
|
||||
gc.ArcTo(xc, yc, 10.0, 10.0, 0, 2*math.Pi)
|
||||
gc.Fill()
|
||||
saveToPngFile("TestDrawArc", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Draw Arc Negative ##
|
||||

|
||||
```
|
||||
func TestDrawArcNegative() {
|
||||
i, gc := initGc(w, h)
|
||||
// draw an arc
|
||||
xc, yc := 128.0, 128.0
|
||||
radiusX, radiusY := 100.0, 100.0
|
||||
startAngle := 45.0 * (math.Pi / 180.0) /* angles are specified */
|
||||
angle := -225 * (math.Pi / 180.0) /* in radians */
|
||||
gc.SetLineWidth(10)
|
||||
gc.SetLineCap(draw2d.ButtCap)
|
||||
gc.SetStrokeColor(image.Black)
|
||||
gc.ArcTo(xc, yc, radiusX, radiusY, startAngle, angle)
|
||||
gc.Stroke()
|
||||
// fill a circle
|
||||
gc.SetStrokeColor(image.RGBAColor{255, 0x33, 0x33, 0x80})
|
||||
gc.SetFillColor(image.RGBAColor{255, 0x33, 0x33, 0x80})
|
||||
gc.SetLineWidth(6)
|
||||
|
||||
gc.MoveTo(xc, yc)
|
||||
gc.LineTo(xc+cos(startAngle)*radiusX, yc+sin(startAngle)*radiusY)
|
||||
gc.MoveTo(xc, yc)
|
||||
gc.LineTo(xc-radiusX, yc)
|
||||
gc.Stroke()
|
||||
|
||||
gc.ArcTo(xc, yc, 10.0, 10.0, 0, 2*math.Pi)
|
||||
gc.Fill()
|
||||
saveToPngFile("TestDrawArcNegative", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Curve Rectangle ##
|
||||

|
||||
```
|
||||
func TestCurveRectangle() {
|
||||
i, gc := initGc(w, h)
|
||||
|
||||
/* a custom shape that could be wrapped in a function */
|
||||
x0, y0 := 25.6, 25.6 /* parameters like cairo_rectangle */
|
||||
rect_width, rect_height := 204.8, 204.8
|
||||
radius := 102.4 /* and an approximate curvature radius */
|
||||
|
||||
x1 := x0 + rect_width
|
||||
y1 := y0 + rect_height
|
||||
if rect_width/2 < radius {
|
||||
if rect_height/2 < radius {
|
||||
gc.MoveTo(x0, (y0+y1)/2)
|
||||
gc.CubicCurveTo(x0, y0, x0, y0, (x0+x1)/2, y0)
|
||||
gc.CubicCurveTo(x1, y0, x1, y0, x1, (y0+y1)/2)
|
||||
gc.CubicCurveTo(x1, y1, x1, y1, (x1+x0)/2, y1)
|
||||
gc.CubicCurveTo(x0, y1, x0, y1, x0, (y0+y1)/2)
|
||||
} else {
|
||||
gc.MoveTo(x0, y0+radius)
|
||||
gc.CubicCurveTo(x0, y0, x0, y0, (x0+x1)/2, y0)
|
||||
gc.CubicCurveTo(x1, y0, x1, y0, x1, y0+radius)
|
||||
gc.LineTo(x1, y1-radius)
|
||||
gc.CubicCurveTo(x1, y1, x1, y1, (x1+x0)/2, y1)
|
||||
gc.CubicCurveTo(x0, y1, x0, y1, x0, y1-radius)
|
||||
}
|
||||
} else {
|
||||
if rect_height/2 < radius {
|
||||
gc.MoveTo(x0, (y0+y1)/2)
|
||||
gc.CubicCurveTo(x0, y0, x0, y0, x0+radius, y0)
|
||||
gc.LineTo(x1-radius, y0)
|
||||
gc.CubicCurveTo(x1, y0, x1, y0, x1, (y0+y1)/2)
|
||||
gc.CubicCurveTo(x1, y1, x1, y1, x1-radius, y1)
|
||||
gc.LineTo(x0+radius, y1)
|
||||
gc.CubicCurveTo(x0, y1, x0, y1, x0, (y0+y1)/2)
|
||||
} else {
|
||||
gc.MoveTo(x0, y0+radius)
|
||||
gc.CubicCurveTo(x0, y0, x0, y0, x0+radius, y0)
|
||||
gc.LineTo(x1-radius, y0)
|
||||
gc.CubicCurveTo(x1, y0, x1, y0, x1, y0+radius)
|
||||
gc.LineTo(x1, y1-radius)
|
||||
gc.CubicCurveTo(x1, y1, x1, y1, x1-radius, y1)
|
||||
gc.LineTo(x0+radius, y1)
|
||||
gc.CubicCurveTo(x0, y1, x0, y1, x0, y1-radius)
|
||||
}
|
||||
}
|
||||
gc.Close()
|
||||
|
||||
gc.SetFillColor(image.RGBAColor{0x80, 0x80, 0xFF, 0xFF})
|
||||
gc.SetStrokeColor(image.RGBAColor{0x80, 0, 0, 0x80})
|
||||
gc.SetLineWidth(10.0)
|
||||
gc.FillStroke()
|
||||
|
||||
saveToPngFile("TestCurveRectangle", i)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Test Draw Cubic Curve ##
|
||||

|
||||
```
|
||||
func TestDrawCubicCurve() {
|
||||
i, gc := initGc(w, h)
|
||||
// draw a cubic curve
|
||||
x, y := 25.6, 128.0
|
||||
x1, y1 := 102.4, 230.4
|
||||
x2, y2 := 153.6, 25.6
|
||||
x3, y3 := 230.4, 128.0
|
||||
|
||||
gc.SetFillColor(image.RGBAColor{0xAA, 0xAA, 0xAA, 0xFF})
|
||||
gc.SetLineWidth(10)
|
||||
gc.MoveTo(x, y)
|
||||
gc.CubicCurveTo(x1, y1, x2, y2, x3, y3)
|
||||
gc.Stroke()
|
||||
|
||||
gc.SetStrokeColor(image.RGBAColor{0xFF, 0x33, 0x33, 0x88})
|
||||
|
||||
gc.SetLineWidth(6)
|
||||
// draw segment of curve
|
||||
gc.MoveTo(x, y)
|
||||
gc.LineTo(x1, y1)
|
||||
gc.LineTo(x2, y2)
|
||||
gc.LineTo(x3, y3)
|
||||
gc.Stroke()
|
||||
saveToPngFile("TestDrawCubicCurve", i)
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
## Test Dash ##
|
||||

|
||||
```
|
||||
func TestDash() {
|
||||
i, gc := initGc(w, h)
|
||||
gc.SetLineDash([]float{50, 10, 10, 10}, -50.0)
|
||||
gc.SetLineCap(draw2d.ButtCap)
|
||||
gc.SetLineJoin(draw2d.BevelJoin)
|
||||
gc.SetLineWidth(10)
|
||||
|
||||
gc.MoveTo(128.0, 25.6)
|
||||
gc.LineTo(128.0, 25.6)
|
||||
gc.LineTo(230.4, 230.4)
|
||||
gc.RLineTo(-102.4, 0.0)
|
||||
gc.CubicCurveTo(51.2, 230.4, 51.2, 128.0, 128.0, 128.0)
|
||||
gc.Stroke()
|
||||
gc.SetLineDash(nil, 0.0)
|
||||
saveToPngFile("TestDash", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Fill Stroke ##
|
||||

|
||||
```
|
||||
func TestFillStroke() {
|
||||
i, gc := initGc(w, h)
|
||||
gc.MoveTo(128.0, 25.6)
|
||||
gc.LineTo(230.4, 230.4)
|
||||
gc.RLineTo(-102.4, 0.0)
|
||||
gc.CubicCurveTo(51.2, 230.4, 51.2, 128.0, 128.0, 128.0)
|
||||
gc.Close()
|
||||
|
||||
gc.MoveTo(64.0, 25.6)
|
||||
gc.RLineTo(51.2, 51.2)
|
||||
gc.RLineTo(-51.2, 51.2)
|
||||
gc.RLineTo(-51.2, -51.2)
|
||||
gc.Close()
|
||||
|
||||
gc.SetLineWidth(10.0)
|
||||
gc.SetFillColor(image.RGBAColor{0, 0, 0xFF, 0xFF})
|
||||
gc.SetStrokeColor(image.Black)
|
||||
gc.FillStroke()
|
||||
saveToPngFile("TestFillStroke", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Fill Style ##
|
||||

|
||||
```
|
||||
func TestFillStyle() {
|
||||
i, gc := initGc(w, h)
|
||||
gc.SetLineWidth(6)
|
||||
|
||||
gc.Rect(12, 12, 244, 70)
|
||||
|
||||
wheel1 := new(draw2d.Path)
|
||||
wheel1.ArcTo(64, 64, 40, 40, 0, 2*math.Pi)
|
||||
wheel2 := new(draw2d.Path)
|
||||
wheel2.ArcTo(192, 64, 40, 40, 0, 2*math.Pi)
|
||||
|
||||
gc.SetFillRule(draw2d.FillRuleEvenOdd)
|
||||
gc.SetFillColor(image.RGBAColor{0, 0xB2, 0, 0xFF})
|
||||
|
||||
gc.SetStrokeColor(image.Black)
|
||||
gc.FillStroke(wheel1, wheel2)
|
||||
|
||||
gc.Rect(12, 140, 244, 198)
|
||||
wheel1 = new(draw2d.Path)
|
||||
wheel1.ArcTo(64, 192, 40, 40, 0, 2*math.Pi)
|
||||
wheel2 = new(draw2d.Path)
|
||||
wheel2.ArcTo(192, 192, 40, 40, 0, -2*math.Pi)
|
||||
|
||||
gc.SetFillRule(draw2d.FillRuleWinding)
|
||||
gc.SetFillColor(image.RGBAColor{0, 0, 0xE5, 0xFF})
|
||||
gc.FillStroke(wheel1, wheel2)
|
||||
saveToPngFile("TestFillStyle", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Multi Segment Caps ##
|
||||

|
||||
```
|
||||
func TestMultiSegmentCaps() {
|
||||
i, gc := initGc(w, h)
|
||||
gc.MoveTo(50.0, 75.0)
|
||||
gc.LineTo(200.0, 75.0)
|
||||
|
||||
gc.MoveTo(50.0, 125.0)
|
||||
gc.LineTo(200.0, 125.0)
|
||||
|
||||
gc.MoveTo(50.0, 175.0)
|
||||
gc.LineTo(200.0, 175.0)
|
||||
|
||||
gc.SetLineWidth(30.0)
|
||||
gc.SetLineCap(draw2d.RoundCap)
|
||||
gc.Stroke()
|
||||
saveToPngFile("TestMultiSegmentCaps", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Round Rectangle ##
|
||||

|
||||
```
|
||||
func TestRoundRectangle() {
|
||||
i, gc := initGc(w, h)
|
||||
/* a custom shape that could be wrapped in a function */
|
||||
x, y := 25.6, 25.6
|
||||
width, height := 204.8, 204.8
|
||||
aspect := 1.0 /* aspect ratio */
|
||||
corner_radius := height / 10.0 /* and corner curvature radius */
|
||||
|
||||
radius := corner_radius / aspect
|
||||
degrees := math.Pi / 180.0
|
||||
|
||||
gc.ArcTo(x+width-radius, y+radius, radius, radius, -90*degrees, 90*degrees)
|
||||
gc.ArcTo(x+width-radius, y+height-radius, radius, radius, 0*degrees, 90*degrees)
|
||||
gc.ArcTo(x+radius, y+height-radius, radius, radius, 90*degrees, 90*degrees)
|
||||
gc.ArcTo(x+radius, y+radius, radius, radius, 180*degrees, 90*degrees)
|
||||
gc.Close()
|
||||
|
||||
gc.SetFillColor(image.RGBAColor{0x80, 0x80, 0xFF, 0xFF})
|
||||
gc.SetStrokeColor(image.RGBAColor{0x80, 0, 0, 0x80})
|
||||
gc.SetLineWidth(10.0)
|
||||
gc.FillStroke()
|
||||
|
||||
saveToPngFile("TestRoundRectangle", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Line Cap ##
|
||||

|
||||
```
|
||||
func TestLineCap() {
|
||||
i, gc := initGc(w, h)
|
||||
gc.SetLineWidth(30.0)
|
||||
gc.SetLineCap(draw2d.ButtCap)
|
||||
gc.MoveTo(64.0, 50.0)
|
||||
gc.LineTo(64.0, 200.0)
|
||||
gc.Stroke()
|
||||
gc.SetLineCap(draw2d.RoundCap)
|
||||
gc.MoveTo(128.0, 50.0)
|
||||
gc.LineTo(128.0, 200.0)
|
||||
gc.Stroke()
|
||||
gc.SetLineCap(draw2d.SquareCap)
|
||||
gc.MoveTo(192.0, 50.0)
|
||||
gc.LineTo(192.0, 200.0)
|
||||
gc.Stroke()
|
||||
|
||||
/* draw helping lines */
|
||||
gc.SetStrokeColor(image.RGBAColor{0xFF, 0x33, 0x33, 0xFF})
|
||||
gc.SetLineWidth(2.56)
|
||||
gc.MoveTo(64.0, 50.0)
|
||||
gc.LineTo(64.0, 200.0)
|
||||
gc.MoveTo(128.0, 50.0)
|
||||
gc.LineTo(128.0, 200.0)
|
||||
gc.MoveTo(192.0, 50.0)
|
||||
gc.LineTo(192.0, 200.0)
|
||||
gc.Stroke()
|
||||
saveToPngFile("TestLineCap", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Line Join ##
|
||||

|
||||
```
|
||||
func TestLineJoin() {
|
||||
i, gc := initGc(w, h)
|
||||
gc.SetLineWidth(40.96)
|
||||
gc.MoveTo(76.8, 84.48)
|
||||
gc.RLineTo(51.2, -51.2)
|
||||
gc.RLineTo(51.2, 51.2)
|
||||
gc.SetLineJoin(draw2d.MiterJoin) /* default */
|
||||
gc.Stroke()
|
||||
|
||||
gc.MoveTo(76.8, 161.28)
|
||||
gc.RLineTo(51.2, -51.2)
|
||||
gc.RLineTo(51.2, 51.2)
|
||||
gc.SetLineJoin(draw2d.BevelJoin)
|
||||
gc.Stroke()
|
||||
|
||||
gc.MoveTo(76.8, 238.08)
|
||||
gc.RLineTo(51.2, -51.2)
|
||||
gc.RLineTo(51.2, 51.2)
|
||||
gc.SetLineJoin(draw2d.RoundJoin)
|
||||
gc.Stroke()
|
||||
saveToPngFile("TestLineJoin", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Bubble ##
|
||||

|
||||
```
|
||||
func TestBubble() {
|
||||
i, gc := initGc(w, h)
|
||||
gc.BeginPath();
|
||||
gc.MoveTo(75,25);
|
||||
gc.QuadCurveTo(25,25,25,62.5);
|
||||
gc.QuadCurveTo(25,100,50,100);
|
||||
gc.QuadCurveTo(50,120,30,125);
|
||||
gc.QuadCurveTo(60,120,65,100);
|
||||
gc.QuadCurveTo(125,100,125,62.5);
|
||||
gc.QuadCurveTo(125,25,75,25);
|
||||
gc.Stroke();
|
||||
saveToPngFile("TestBubble", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Star ##
|
||||

|
||||
```
|
||||
func TestStar() {
|
||||
i, gc := initGc(w, h)
|
||||
for i := 0.0 ; i < 360; i = i + 10 {// Go from 0 to 360 degrees in 10 degree steps
|
||||
gc.Save()
|
||||
gc.SetLineWidth(5) // Keep rotations temporary
|
||||
gc.Translate(144, 144)
|
||||
gc.Rotate(i * (math.Pi / 180.0)) // Rotate by degrees on stack from 'for'
|
||||
gc.MoveTo(0, 0)
|
||||
gc.LineTo(72, 0)
|
||||
gc.Stroke()
|
||||
gc.Restore()
|
||||
}
|
||||
saveToPngFile("TestStar", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Transform ##
|
||||

|
||||
```
|
||||
|
||||
func TestTransform() {
|
||||
i, gc := initGc(800, 600)
|
||||
|
||||
gc.Save()
|
||||
gc.Translate(40, 40) // Set origin to (40, 40)
|
||||
gc.BeginPath()
|
||||
gc.MoveTo(0,0)
|
||||
gc.RLineTo(72,0)
|
||||
gc.RLineTo(0, 72)
|
||||
gc.RLineTo(-72,0)
|
||||
gc.ClosePath()
|
||||
gc.Stroke()
|
||||
gc.Restore()
|
||||
|
||||
gc.Save()
|
||||
gc.Translate(100, 150) // Translate origin to (100, 150)
|
||||
gc.Rotate(30* (math.Pi / 180.0)) // Rotate counter-clockwise by 30 degrees
|
||||
gc.BeginPath()
|
||||
gc.MoveTo(0,0)
|
||||
gc.RLineTo(72,0)
|
||||
gc.RLineTo(0, 72)
|
||||
gc.RLineTo(-72,0)
|
||||
gc.ClosePath() // Draw box...
|
||||
gc.Stroke()
|
||||
gc.Restore()
|
||||
|
||||
gc.Save()
|
||||
gc.Translate(40, 300) // Translate to (40, 300)
|
||||
gc.Scale(0.5, 1) // Reduce x coord by 1/2, y coord left alone
|
||||
gc.BeginPath()
|
||||
gc.MoveTo(0,0)
|
||||
gc.RLineTo(72,0)
|
||||
gc.RLineTo(0, 72)
|
||||
gc.RLineTo(-72,0)
|
||||
gc.ClosePath() // Draw box...
|
||||
gc.Stroke()
|
||||
gc.Restore()
|
||||
|
||||
gc.Save()
|
||||
gc.Translate(300, 300) // Set origin to (300, 300)
|
||||
gc.Rotate(45* (math.Pi / 180.0)) // Rotate coordinates by 45 degrees
|
||||
gc.Scale(0.5, 1) // Scale coordinates
|
||||
gc.BeginPath()
|
||||
gc.MoveTo(0,0)
|
||||
gc.RLineTo(72,0)
|
||||
gc.RLineTo(0, 72)
|
||||
gc.RLineTo(-72,0)
|
||||
gc.ClosePath() // Draw box
|
||||
gc.Stroke()
|
||||
gc.Restore()
|
||||
|
||||
saveToPngFile("TestTransform", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Path Transform ##
|
||||

|
||||
```
|
||||
|
||||
func TestPathTransform() {
|
||||
i, gc := initGc(800, 600)
|
||||
gc.SetLineWidth(20)
|
||||
gc.Scale(1,5)
|
||||
gc.ArcTo(200, 50, 50, 50, 0, math.Pi * 2)
|
||||
gc.Stroke()
|
||||
saveToPngFile("TestPathTransform", i)
|
||||
}
|
||||
```
|
||||
|
||||
## Test Android ##
|
||||

|
||||
```
|
||||
|
||||
func android(gc *draw2d.GraphicContext, x, y float) {
|
||||
gc.SetLineCap(draw2d.RoundCap)
|
||||
gc.SetLineWidth(5)
|
||||
gc.ArcTo(x+80, y+70, 50, 50, 180 * (math.Pi/180), 360 * (math.Pi/180)) // head
|
||||
gc.FillStroke()
|
||||
gc.MoveTo(x+60, y+25)
|
||||
gc.LineTo(x+50, y+10)
|
||||
gc.MoveTo(x+100, y+25)
|
||||
gc.LineTo( x+110, y+10)
|
||||
gc.Stroke()
|
||||
draw2d.Circle(gc, x+60, y+45, 5) // left eye
|
||||
gc.FillStroke()
|
||||
draw2d.Circle(gc, x+100, y+45, 5) // right eye
|
||||
gc.FillStroke()
|
||||
draw2d.RoundRect(gc, x+30, y+75, x+30+100, y+75+90, 10, 10) // body
|
||||
gc.FillStroke()
|
||||
draw2d.Rect(gc, x+30, y+75, x+30+100, y+75+80)
|
||||
gc.FillStroke()
|
||||
draw2d.RoundRect(gc, x+5, y+80, x+5+20, y+80+70, 10, 10) // left arm
|
||||
gc.FillStroke()
|
||||
draw2d.RoundRect(gc, x+135, y+80, x+135+20, y+80+70, 10, 10) // right arm
|
||||
gc.FillStroke()
|
||||
draw2d.RoundRect(gc, x+50, y+150, x+50+20, y+150+50, 10, 10) // left leg
|
||||
gc.FillStroke()
|
||||
draw2d.RoundRect(gc, x+90, y+150, x+90+20, y+150+50, 10, 10) // right leg
|
||||
gc.FillStroke()
|
||||
}
|
||||
```
|
||||
## Test Gopher ##
|
||||

|
||||
```
|
||||
|
||||
func gordon(gc *draw2d.GraphicContext, x, y, w, h float) {
|
||||
h23 := (h * 2) / 3
|
||||
|
||||
blf := image.RGBAColor{0, 0, 0, 0xff}
|
||||
wf := image.RGBAColor{0xff, 0xff, 0xff, 0xff}
|
||||
nf := image.RGBAColor{0x8B, 0x45, 0x13, 0xff}
|
||||
brf := image.RGBAColor{0x8B, 0x45, 0x13, 0x99}
|
||||
brb := image.RGBAColor{0x8B, 0x45, 0x13, 0xBB}
|
||||
|
||||
gc.MoveTo(x, y+h)
|
||||
gc.CubicCurveTo(x, y+h, x+w/2, y-h, x+w, y+h)
|
||||
gc.Close()
|
||||
gc.SetFillColor(brb)
|
||||
gc.Fill()
|
||||
draw2d.RoundRect(gc, x, y+h, x+ w, y+h+h, 10, 10)
|
||||
gc.Fill()
|
||||
draw2d.Circle(gc,x, y+h, w/12) // left ear
|
||||
gc.SetFillColor(brf)
|
||||
gc.Fill()
|
||||
draw2d.Circle(gc, x, y+h, w/12-10)
|
||||
gc.SetFillColor(nf)
|
||||
gc.Fill()
|
||||
|
||||
draw2d.Circle(gc, x+w, y+h, w/12) // right ear
|
||||
gc.SetFillColor(brf)
|
||||
gc.Fill()
|
||||
draw2d.Circle(gc, x+w, y+h, w/12-10)
|
||||
gc.SetFillColor(nf)
|
||||
gc.Fill()
|
||||
|
||||
draw2d.Circle(gc, x+w/3, y+h23, w/9) // left eye
|
||||
gc.SetFillColor(wf)
|
||||
gc.Fill()
|
||||
draw2d.Circle(gc, x+w/3+10, y+h23, w / 10 - 10)
|
||||
gc.SetFillColor(blf)
|
||||
gc.Fill()
|
||||
draw2d.Circle(gc, x+w/3+15, y+h23, 5)
|
||||
gc.SetFillColor(wf)
|
||||
gc.Fill()
|
||||
|
||||
draw2d.Circle(gc, x+w-w/3, y+h23, w/9) // right eye
|
||||
gc.Fill()
|
||||
draw2d.Circle(gc, x+w-w/3+10, y+h23, w / 10 - 10)
|
||||
gc.SetFillColor(blf)
|
||||
gc.Fill()
|
||||
draw2d.Circle(gc, x+w-(w/3)+15, y+h23, 5)
|
||||
gc.SetFillColor(wf)
|
||||
gc.Fill()
|
||||
|
||||
gc.SetFillColor(wf)
|
||||
draw2d.RoundRect(gc, x+w/2-w/8, y+h+30, x+w/2-w/8 + w/8, y+h+30 + w/6, 5, 5) // left tooth
|
||||
gc.Fill()
|
||||
draw2d.RoundRect(gc, x+w/2, y+h+30, x+w/2+w/8, y+h+30+w/6, 5, 5) // right tooth
|
||||
gc.Fill()
|
||||
|
||||
|
||||
draw2d.Ellipse(gc, x+(w/2), y+h+30, w/6, w/12) // snout
|
||||
gc.SetFillColor(nf)
|
||||
gc.Fill()
|
||||
draw2d.Ellipse(gc, x+(w/2), y+h+10, w/10, w/12) // nose
|
||||
gc.SetFillColor(blf)
|
||||
gc.Fill()
|
||||
|
||||
}
|
||||
```
|
||||
## Test Fill String ##
|
||||

|
||||
```
|
||||
func TestFillString() {
|
||||
i, gc := initGc(100, 100)
|
||||
draw2d.RoundRect(gc, 5, 5, 95, 95, 10, 10)
|
||||
gc.FillStroke()
|
||||
gc.SetFontSize(18)
|
||||
gc.MoveTo(10, 52)
|
||||
gc.SetFontData(draw2d.FontData{"luxi", draw2d.FontFamilyMono, draw2d.FontStyleBold|draw2d.FontStyleItalic})
|
||||
width := gc.FillString("cou")
|
||||
fmt.Printf("width: %f\n", width)
|
||||
gc.RMoveTo(width+1, 0)
|
||||
gc.FillString("cou")
|
||||
saveToPngFile("TestFillString", i)
|
||||
}
|
||||
```
|
62
matrix.md
Normal file
62
matrix.md
Normal file
|
@ -0,0 +1,62 @@
|
|||
### 3D Matrix transformation decomposition ###
|
||||
```
|
||||
M11 M12 M13 M14
|
||||
M21 M22 M23 M24
|
||||
M31 M32 M33 M34
|
||||
```
|
||||
|
||||
### Identity ###
|
||||
```
|
||||
1 0 0 0
|
||||
0 1 0 0
|
||||
0 0 1 0
|
||||
```
|
||||
|
||||
### Translation Vector ###
|
||||
```
|
||||
vt = (M13 M23 M34)
|
||||
```
|
||||
|
||||
### Scaling coefficient ###
|
||||
```
|
||||
sx = sqrt(M11² + M12² + M13²);
|
||||
sy = sqrt(M21² + M22² + M23²);
|
||||
sz = sqrt(M31² + M32² + M33²);
|
||||
```
|
||||
|
||||
### Rotation Matrix ###
|
||||
```
|
||||
M11/sx M12/sx M13/sx 0
|
||||
M21/sy M22/sy M23/sy 0
|
||||
M31/sz M32/sz M33/sz 0
|
||||
```
|
||||
|
||||
### 2D Matrix transformation decomposition ###
|
||||
```
|
||||
M11 M12 M13
|
||||
M21 M22 M23
|
||||
```
|
||||
|
||||
### Identity ###
|
||||
```
|
||||
1 0 0
|
||||
0 1 0
|
||||
```
|
||||
|
||||
### Translation Vector ###
|
||||
```
|
||||
vt = (M13 M23)
|
||||
```
|
||||
|
||||
### Scaling coefficient ###
|
||||
```
|
||||
sx = sqrt(M11² + M12²);
|
||||
sy = sqrt(M21² + M22²);
|
||||
sz = sqrt(M31² + M32²);
|
||||
```
|
||||
|
||||
### Rotation Matrix ###
|
||||
```
|
||||
M11/sx M12/sx 0
|
||||
M21/sy M22/sy 0
|
||||
```
|
19
news.md
Normal file
19
news.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
# How to use draw2d with Gotour #
|
||||
[Draw2d with Gotour](Gotour.md)
|
||||
|
||||
# draw2d and opengl #
|
||||
date: 5/15/2011 author: Laurent Le Goff
|
||||
|
||||
I'ts now possible to use the common GraphicContext to display 2d figure on opengl. The particularity of this implementationis that it is compatible with old opengl implementation. And either the graphic card the rendering is the same. I use Vextex buffer to draw hspan with the freetype Painter.
|
||||
You can test the opengl implementation (tested on windows and macosx) http://code.google.com/p/draw2d/source/browse/cmd/draw2dgl.go
|
||||
|
||||
If you test this code please tell me about your metrics displayed in console.
|
||||
|
||||
|
||||
# Test draw2d on windows 7 #
|
||||
date: 2/15/2011 author: Laurent Le Goff
|
||||
|
||||
I've recently installed go on a windows 7 machine.
|
||||
I've tested the possibility of using the wingui and added some support for bitmap.
|
||||
My first simple test works nice the Go program parse a postscript file and display it in a window on win7.
|
||||

|
Loading…
Add table
Reference in a new issue