fogleman / gg

Go Graphics - 2D rendering in Go with a simple API.
https://godoc.org/github.com/fogleman/gg
MIT License
4.39k stars 355 forks source link

Question: How to draw multi image with clip or scale on context? #56

Closed cospotato closed 5 years ago

cospotato commented 5 years ago

Firstly, thank you for the great work, it is helpful on drawing with Golang!

Code

package main

import (
    "log"

    "github.com/fogleman/gg"
)

func main() {
    im, err := gg.LoadImage("examples/lenna.png")
    if err != nil {
        log.Fatal(err)
    }

    dc := gg.NewContext(1024, 1024)
    dc.DrawRoundedRectangle(0, 0, 512, 512, 64)
    dc.Clip()
    dc.DrawImage(im, 0, 0)
    dc.DrawRoundedRectangle(512, 512, 512, 512, 64)
    dc.Clip()
    dc.DrawImage(im, 512, 512)
    dc.SavePNG("out.png")
}

Image

image

Wish

image

Best Wishes, CosPotato

cospotato commented 5 years ago

OK, now this code working.

package main

import (
    "log"

    "github.com/fogleman/gg"
)

func main() {
    im, err := gg.LoadImage("examples/lenna.png")
    if err != nil {
        log.Fatal(err)
    }

    // Draw image1
    dc := gg.NewContext(1024, 1024)
    tmpDc1 := gg.NewContext(512, 512)
    tmpDc1.DrawRoundedRectangle(0, 0, 512, 512, 64)
    tmpDc1.Clip()
    tmpDc1.DrawImage(im, 0, 0)
    dc.DrawImage(tmpDc1.Image(), 0, 0)

    // Draw image2
    tmpDc2 := gg.NewContext(512, 512)
    tmpDc2.DrawRoundedRectangle(0, 0, 512, 512, 64)
    tmpDc2.Clip()
    tmpDc2.DrawImage(im, 0, 0)
    dc.DrawImage(tmpDc2.Image(), 512, 512)
    dc.SavePNG("out.png")
}

Any best practice ?

fogleman commented 5 years ago

@Cospotato You can use ResetClip to clear the clipping region before setting a new one.

cospotato commented 5 years ago

@fogleman Thank you for your reply! I have another question: How to DrawImage with transparent mask ?