Пример #1
0
func generatePie(params PieParams) image.Image {
	img := image.NewRGBA(image.Rect(0, 0, params.size, params.size))

	progressColor := progressColors[params.color]
	halfWidth := float64(params.size) * 0.5
	strokeWidth := math.Floor(halfWidth * 0.1)
	circleSize := halfWidth * 0.94
	arcAngle := 6.283185 // 2 * math.Pi

	gc := draw2dimg.NewGraphicContext(img)
	gc.BeginPath()
	gc.SetStrokeColor(progressColor)
	gc.SetFillColor(white)
	gc.SetLineWidth(strokeWidth)
	gc.ArcTo(halfWidth, halfWidth, circleSize, circleSize, arcAngle, arcAngle)
	gc.Close()
	gc.FillStroke()

	startAngle := 270 * (math.Pi / 180.0)
	angle := (360 * (float64(params.progress) * 0.01)) * (math.Pi / 180.0)
	gc.SetFillColor(progressColor)
	gc.BeginPath()
	gc.MoveTo(halfWidth, halfWidth)
	gc.ArcTo(halfWidth, halfWidth, circleSize, circleSize, startAngle, angle)
	gc.Close()
	gc.Fill()

	return img
}
Пример #2
0
func generateImage(params Params) image.Image {
	fgColor := white

	var detectedFont *truetype.Font
	if []rune(params.text)[0] > '\u2E7F' {
		detectedFont = cjkFont
	} else {
		detectedFont = defaultFont
	}

	fontSize := float64(params.size) * 0.5

	img := image.NewRGBA(image.Rect(0, 0, params.size, params.size))

	if params.seed != 0 {
		rand.Seed(params.seed)
	}

	var bgColor color.RGBA
	if params.color == (color.RGBA{}) {
		bgColor = colors[rand.Intn(len(colors))]
	} else {
		bgColor = params.color
	}

	if params.border {
		bgColor, fgColor = fgColor, bgColor
	}

	draw.Draw(img, img.Bounds(), &image.Uniform{bgColor}, image.ZP, draw.Src)

	if params.border {
		strokeWidth := fontSize * 0.08
		circleSize := fontSize * 0.92
		arcAngle := math.Pi * 2

		gc := draw2dimg.NewGraphicContext(img)
		gc.SetStrokeColor(fgColor)
		gc.SetLineWidth(strokeWidth)
		gc.ArcTo(fontSize, fontSize, circleSize, circleSize, arcAngle, arcAngle)
		gc.Stroke()
	}

	d := &font.Drawer{
		Dst: img,
		Src: image.NewUniform(fgColor),
		Face: truetype.NewFace(detectedFont, &truetype.Options{
			Size: fontSize,
			DPI:  72,
		}),
	}
	d.Dot = fixed.Point26_6{
		X: (fixed.I(params.size) - d.MeasureString(params.text)) / 2,
		Y: fixed.I(int(math.Ceil(fontSize * 1.35))),
	}
	d.DrawString(params.text)

	return img
}
Пример #3
0
func test(t *testing.T, draw sample) {
	// Initialize the graphic context on an RGBA image
	dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0))
	gc := draw2dimg.NewGraphicContext(dest)
	// Draw Android logo
	output, err := draw(gc, "png")
	if err != nil {
		t.Errorf("Drawing %q failed: %v", output, err)
		return
	}
	// Save to png
	err = draw2dimg.SaveToPngFile(output, dest)
	if err != nil {
		t.Errorf("Saving %q failed: %v", output, err)
	}
}
Пример #4
0
func imgPng(w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-type", "image/png")

	// Initialize the graphic context on an RGBA image
	dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0))
	gc := draw2dimg.NewGraphicContext(dest)

	// Draw sample
	android.Draw(gc, 65, 0)

	err := png.Encode(w, dest)
	if err != nil {
		return &appError{err, fmt.Sprintf("Can't encode: %s", err), 500}
	}

	return nil
}
Пример #5
0
func TestCircle(t *testing.T) {
	width := 200
	height := 200
	img := image.NewRGBA(image.Rect(0, 0, width, height))
	gc := draw2dimg.NewGraphicContext(img)

	gc.SetStrokeColor(color.NRGBA{255, 255, 255, 255})
	gc.SetFillColor(color.NRGBA{255, 255, 255, 255})
	gc.Clear()

	gc.SetStrokeColor(color.NRGBA{255, 0, 0, 255})
	gc.SetLineWidth(1)

	// Draw a circle
	Circle(gc, 100, 100, 50)
	gc.Stroke()

	draw2dimg.SaveToPngFile("../output/draw2dkit/TestCircle.png", img)
}
Пример #6
0
func generateHorseshoe(params PieParams) image.Image {
	img := image.NewRGBA(image.Rect(0, 0, params.size, params.size))

	progressColor := progressColors[params.color]
	progressShadow := progressShadows[params.color]

	halfWidth := float64(params.size / 2)
	strokeWidth := float64(params.size) * 0.086
	bigStrokeWidth := float64(params.size) * 0.1
	circleSize := halfWidth * 0.71
	bigCircleSize := float64(params.size) * 0.4
	startAngle := 120 * (math.Pi / 180.0)
	angle := (300 * (float64(100) * 0.01)) * (math.Pi / 180.0)

	gc := draw2dimg.NewGraphicContext(img)
	gc.BeginPath()
	gc.SetStrokeColor(gray)
	gc.SetLineWidth(strokeWidth)
	gc.ArcTo(halfWidth, halfWidth, circleSize, circleSize, startAngle, angle)
	gc.Stroke()

	angle = (300 * (float64(params.progress) * 0.01)) * (math.Pi / 180.0)
	gc.BeginPath()
	gc.SetStrokeColor(progressShadow)
	gc.SetLineWidth(strokeWidth)
	gc.ArcTo(halfWidth, halfWidth, circleSize, circleSize, startAngle, angle)
	gc.Stroke()

	gc.BeginPath()
	gc.SetStrokeColor(progressColor)
	gc.SetLineWidth(bigStrokeWidth)
	gc.ArcTo(halfWidth, halfWidth, bigCircleSize, bigCircleSize, startAngle, angle)
	gc.Stroke()

	return img
}