Build your own Paint
Build your own Paint
Welcome! This tutorial shows you how to write a small drawing app in Rust, in the spirit of Paint. We open a window, draw rectangles, circles and lines with the mouse, move them around, pick a color, and export the drawing as an image. We start from an empty cargo new. By the end, you have about 800 lines of Rust spread over fifteen or so files, with a single dependency: minifb, which opens the window and gives us the keyboard and the mouse. Everything else we write ourselves: filling pixels, drawing a line, checking whether the mouse is on a circle, converting an HSV color to RGB. The tutorial is split into 12 chapters. Each one ends with a program that compiles and does something you can see, even if at first it’s just an image file with a red square in it.
欢迎!本教程将向你展示如何以“画图”软件的精神,用 Rust 编写一个小型的绘图应用程序。我们将打开一个窗口,使用鼠标绘制矩形、圆形和线条,移动它们,选择颜色,并将绘图导出为图像。我们从一个空的 cargo new 项目开始。到最后,你将拥有大约 800 行分布在约 15 个文件中的 Rust 代码,且仅有一个依赖项:minifb,它负责打开窗口并提供键盘和鼠标输入。其他所有内容都由我们自己编写:填充像素、绘制线条、检查鼠标是否在圆圈上、将 HSV 颜色转换为 RGB。本教程分为 12 章。每一章结束时,你都会得到一个可以编译并能看到实际效果的程序,即使起初它只是一个包含红色方块的图像文件。
Why this tutorial# I’m a developer and I’ve been doing data science for several years. But in my free time, I’ve always been drawn to low-level things: bare metal, and how a computer works at the very bottom of the stack. I wrote this tutorial with someone in mind who is starting out, or simply curious, and who wonders how a program ends up showing something on the screen. It covers the basics of rasterization, that is, how a rectangle or a circle turns into pixels, starting from nothing and in small pieces you can understand one at a time. That’s mostly what I’d like to show: by moving forward step by step, in a language close to the machine, you end up with something that works and that you wrote entirely yourself. It takes time, but there’s nothing magic about it. If this tutorial takes away a bit of that mystery and makes a few beginners want to look further down, it will have done its job.
为什么写这个教程?我是一名开发人员,从事数据科学工作已有几年。但在业余时间,我一直被底层事物所吸引:裸机编程,以及计算机在技术栈的最底层是如何工作的。我编写本教程时,心中设想的是那些刚入门或仅仅是出于好奇,想知道程序最终是如何在屏幕上显示内容的人。它涵盖了光栅化的基础知识,即矩形或圆形如何转化为像素,从零开始,并拆解成你可以逐一理解的小部分。这正是我想要展示的:通过一步步前进,使用一种接近机器的语言,你最终会得到一个能运行且完全由你自己编写的程序。这需要时间,但其中并没有什么魔法。如果本教程能消除一点神秘感,并让一些初学者想要深入探索,那么它的目的就达到了。
Who it’s for# This tutorial is a way to learn Rust by building something. I assume you’ve read the first chapters of the Rust Book (variables, functions, if, loops) but haven’t written a real program with it yet. The heavier concepts, like enums, traits, borrowing, modules or error handling, come along chapter by chapter, when we need them. You don’t need to know anything about graphics. As for math, you need Pythagoras for circles and a projection to tell whether the mouse is on a line. I explain it when we get there. Each chapter has boxes that explain why Rust does things the way it does. If you already know, skip them. Same for the exercises at the end of each chapter: they’re there if you want practice, but nothing that follows depends on them. I’d still recommend typing the code rather than copy-pasting it. It’s slower, but typing is how you make mistakes, and fixing them is how you end up understanding what the compiler is trying to tell you.
适合人群:本教程是一种通过构建项目来学习 Rust 的方式。我假设你已经阅读了《Rust 程序设计语言》的前几章(变量、函数、if、循环),但还没有用它写过真正的程序。更复杂的概念,如枚举、特征、借用、模块或错误处理,会随着章节的推进,在我们需要时逐一引入。你不需要了解任何图形学知识。至于数学,你需要勾股定理来处理圆,以及投影知识来判断鼠标是否在直线上。我会在用到时进行解释。每一章都有专门的方框来解释 Rust 为什么以某种方式处理事务。如果你已经知道了,可以跳过它们。每章末尾的练习也是如此:它们是为了让你练习而设置的,但后续内容并不依赖于它们。我仍然建议你手动输入代码,而不是复制粘贴。虽然慢一些,但输入过程会让你犯错,而修复这些错误正是你最终理解编译器试图告诉你什么的方式。
What it isn’t# Let’s be upfront: at the end, you won’t have GIMP. There’s no brush, no layers, no undo. And technically, our app isn’t even really a Paint. Paint paints pixels that can’t be moved once they’re down, whereas we keep a list of shapes that can be selected and moved. We come back to this in chapter 11, and the Going further page has ideas for adding a brush, a paint bucket or undo.
它不是什么:我们先说明白:最后你不会得到一个 GIMP。这里没有画笔、没有图层、没有撤销功能。从技术上讲,我们的应用程序甚至不是真正的“画图”软件。画图软件绘制的是一旦放下就无法移动的像素,而我们维护的是一个可以被选中和移动的形状列表。我们会在第 11 章回到这一点,而“进一步探索”页面提供了添加画笔、油漆桶或撤销功能的思路。
Contents# 00SetupWe install Rust, create the project and check that everything compiles.Duration 15 min012D vectorsWe write a Vec2 type with its operations, and try it out in the terminal.Duration 45 min02An image in memoryWe build an image pixel by pixel and save it in the PPM format.Duration 40 min03Drawing shapesWe pack each color into a single number, and draw rectangles, circles and lines pixel by pixel.Duration 50 min04A windowWe add our first dependency to open a window and keep showing the canvas in it.Duration 35 min05The mouseWe draw rectangles by clicking and dragging, with a preview that follows the mouse.Duration 1 h06Several shapesWe add circles and lines, and choose the tool with the keyboard.Duration 45 min07Selecting and movingWe click on a shape to select it, drag it to move it, and delete it with the keyboard.Duration 1 h 1508The toolbarWe add buttons in the top-right corner of the window to choose the tool with the mouse.Duration 40 min09ColorsWe add a color button that opens a picker, to choose the color of new shapes.Duration 1 h10SavingWe save the drawing to an image file, without the toolbar or the selection, and without overwriting previous files.Duration 30 min11Wrap-upNo code in this chapter. We step back to look at what we built and what we learned.Duration 15 min After that, Going further suggests ideas to keep going on your own, from freehand drawing to a version that runs in the browser.
目录:00 设置:安装 Rust,创建项目并检查一切是否能编译。时长 15 分钟。01 二维向量:编写一个 Vec2 类型及其运算,并在终端中进行测试。时长 45 分钟。02 内存中的图像:逐像素构建图像并以 PPM 格式保存。时长 40 分钟。03 绘制形状:将每种颜色打包成一个数字,并逐像素绘制矩形、圆形和线条。时长 50 分钟。04 窗口:添加第一个依赖项以打开窗口,并持续在其中显示画布。时长 35 分钟。05 鼠标:通过点击和拖动绘制矩形,并带有跟随鼠标的预览效果。时长 1 小时。06 多种形状:添加圆形和线条,并使用键盘选择工具。时长 45 分钟。07 选择与移动:点击形状以选中它,拖动以移动它,并使用键盘删除它。时长 1 小时 15 分钟。08 工具栏:在窗口右上角添加按钮,以便用鼠标选择工具。时长 40 分钟。09 颜色:添加一个打开颜色选择器的按钮,以选择新形状的颜色。时长 1 小时。10 保存:将绘图保存为图像文件,不包含工具栏或选择框,且不覆盖之前的文件。时长 30 分钟。11 总结:本章没有代码。我们回顾所构建的内容和学到的知识。时长 15 分钟。之后,“进一步探索”页面提供了继续自行研究的思路,从手绘功能到在浏览器中运行的版本。
Following along with git# All the code is in the pages, you don’t need anything else. But if you prefer, the GitHub repo has one tag per chapter: git checkout ch05-mouse gives you the exact code at the end of chapter 5, and git diff ch04-window ch05-mouse shows everything it adds. It’s also handy to start again from a clean state if you get lost. The Following along with git page explains all of this. If you find a mistake, an unclear explanation or a piece of code that doesn’t compile, open an issue on GitHub. It really helps. If you’re ready, start here: chapter 0.
使用 git 跟进:所有代码都在页面中,你不需要其他任何东西。但如果你愿意,GitHub 仓库为每一章提供了一个标签:git checkout ch05-mouse 可以让你获得第 5 章结束时的确切代码,而 git diff ch04-window ch05-mouse 可以显示它添加的所有内容。如果你迷失了方向,从干净的状态重新开始也很方便。“使用 git 跟进”页面解释了所有这些内容。如果你发现错误、解释不清的地方或无法编译的代码,请在 GitHub 上提交 issue。这真的很有帮助。如果你准备好了,从这里开始:第 0 章。