开始使用开罗的最快方法

发布于 2024-10-30 12:22:57 字数 167 浏览 1 评论 0原文

我过去曾尝试过学习 Cairo,但总是转向其他一些图形库。我的问题是我找不到一个好的教程来为我的表面提供简单的显示。我总是最终在 GTK 或 QT 文档中挖掘与我想做的事情无关的东西。我想学习开罗,而不是一个庞大的面向对象架构。

什么是一个简单的包装器,可以为我提供一个可以在开罗画布上绘图的跨平台窗口?

I have taken passing shots at learning Cairo in the past, but always moved on in favor of some other graphics library. My problem is that I can't find a good tutorial that gives me a simple display for my surface. I have always ended up digging through GTK or QT documentation about things that have nothing to do with what I want to do. I want to learn Cairo, not a massive OO architecture.

What is a bare bones wrapper to give me a cross-platform window with a Cairo canvas to draw on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不寐倦长更 2024-11-06 12:22:57

我几乎用开罗做任何涉及绘画的事情。我在一家医疗软件公司工作,所以我制作了科学数据可视化和其他东西的原型。

我通常有三种方式来显示我的绘图:

  1. 使用Python脚本和GTK创建的GTK绘图区域;
  2. 使用 Python 图像库 show() 方法直接在屏幕上显示 PNG 图像;
  3. 同样通过 Python 图像库保存到磁盘的 PNG 图像。

一个源自 cairgraphics 示例的简单脚本(实际上我将其用作任何新项目的模板)是:

import gtk

class Canvas(gtk.DrawingArea):
    def __init__(self):
        super(Canvas, self).__init__()
        self.connect("expose_event", self.expose)
        self.set_size_request(800,500)

    def expose(self, widget, event):
        cr = widget.window.cairo_create()
        rect = self.get_allocation()

        # you can use w and h to calculate relative positions which
        # also change dynamically if window gets resized
        w = rect.width
        h = rect.height

        # here is the part where you actually draw
        cr.move_to(0,0)
        cr.line_to(w/2, h/2)
        cr.stroke()

window = gtk.Window()
canvas = Canvas()
window.add(canvas)
window.set_position(gtk.WIN_POS_CENTER)
window.show_all()
gtk.main()

或者,如果您不想处理 GUI 工具包,您可以在屏幕上创建和显示图像,并可选择将其保存到文件中:

import cairo, Image

width = 800
height = 600

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context(surface)

# optional conversion from screen to cartesian coordinates:
cr.translate(0, height)
cr.scale(1, -1)

# something very similar to Japanese flag:
cr.set_source_rgb(1,1,1)
cr.rectangle(0, 0, width, height)
cr.fill()
cr.arc(width/2, height/2, 150, 0, 6.28)
cr.set_source_rgb(1,0,0)
cr.fill()

im = Image.frombuffer("RGBA",
                       (width, height),
                       surface.get_data(),
                       "raw",
                       "BGRA",
                       0,1) # don't ask me what these are!
im.show()
# im.save('filename', 'png')

I have used cairo for virtually anything involving drawing. I work at a medical software company, so I prototype scientific data visualization and other things.

I have usually three ways to display my drawings:

  1. A GTK drawing area created with a Python script and GTK;
  2. A PNG image displayed directly on screen using Python Image Library show() method;
  3. A PNG image saved to disk, also via Python Image Library.

A simple script derived from cairographics examples, which actually I use as a template for any new project, is:

import gtk

class Canvas(gtk.DrawingArea):
    def __init__(self):
        super(Canvas, self).__init__()
        self.connect("expose_event", self.expose)
        self.set_size_request(800,500)

    def expose(self, widget, event):
        cr = widget.window.cairo_create()
        rect = self.get_allocation()

        # you can use w and h to calculate relative positions which
        # also change dynamically if window gets resized
        w = rect.width
        h = rect.height

        # here is the part where you actually draw
        cr.move_to(0,0)
        cr.line_to(w/2, h/2)
        cr.stroke()

window = gtk.Window()
canvas = Canvas()
window.add(canvas)
window.set_position(gtk.WIN_POS_CENTER)
window.show_all()
gtk.main()

Or if you prefer not to deal with GUI toolkits, you can create and display an image on screen, and optionally save it to file:

import cairo, Image

width = 800
height = 600

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context(surface)

# optional conversion from screen to cartesian coordinates:
cr.translate(0, height)
cr.scale(1, -1)

# something very similar to Japanese flag:
cr.set_source_rgb(1,1,1)
cr.rectangle(0, 0, width, height)
cr.fill()
cr.arc(width/2, height/2, 150, 0, 6.28)
cr.set_source_rgb(1,0,0)
cr.fill()

im = Image.frombuffer("RGBA",
                       (width, height),
                       surface.get_data(),
                       "raw",
                       "BGRA",
                       0,1) # don't ask me what these are!
im.show()
# im.save('filename', 'png')
生生漫 2024-11-06 12:22:57

相关问题的答案演示了 Gtk2HS 中非常简单的设置使用 Cairo 在绘图区域上绘图。

import Graphics.UI.Gtk
import Graphics.Rendering.Cairo

main :: IO ()
main = do
    initGUI
    window      <- windowNew
    drawingArea <- drawingAreaNew
    containerAdd window drawingArea

    drawingArea `onExpose` (\_ -> renderScene drawingArea)
    window `onDestroy` mainQuit

    windowSetDefaultSize window 640 480
    widgetShowAll window
    mainGUI

renderScene :: DrawingArea -> IO Bool
renderScene da = do
    dw <- widgetGetDrawWindow da
    renderWithDrawable dw $ do setSourceRGBA 0.5 0.5 0.5 1.0
                               moveTo 100.0 100.0
                               showText "HelloWorld"

    return True

只需将您的 Cairo 动画例程传递给 renderScene 中的 renderWithDrawable dw 即可。

An answer to a related question demonstrates a very simple setup in Gtk2HS to draw on a drawingArea with Cairo.

import Graphics.UI.Gtk
import Graphics.Rendering.Cairo

main :: IO ()
main = do
    initGUI
    window      <- windowNew
    drawingArea <- drawingAreaNew
    containerAdd window drawingArea

    drawingArea `onExpose` (\_ -> renderScene drawingArea)
    window `onDestroy` mainQuit

    windowSetDefaultSize window 640 480
    widgetShowAll window
    mainGUI

renderScene :: DrawingArea -> IO Bool
renderScene da = do
    dw <- widgetGetDrawWindow da
    renderWithDrawable dw $ do setSourceRGBA 0.5 0.5 0.5 1.0
                               moveTo 100.0 100.0
                               showText "HelloWorld"

    return True

Simply pass your Cairo animation routine to renderWithDrawable dw in renderScene.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文