unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Po Lu <luangruo@yahoo.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: emacs-devel@gnu.org
Subject: Re: Emacs canvas support
Date: Wed, 29 Apr 2020 17:57:03 +0800	[thread overview]
Message-ID: <87zhau1uog.fsf@yahoo.com> (raw)
In-Reply-To: <834kt21yyo.fsf@gnu.org> (Eli Zaretskii's message of "Wed, 29 Apr 2020 11:24:31 +0300")

[-- Attachment #1: Type: text/plain, Size: 1847 bytes --]

Eli Zaretskii <eliz@gnu.org> writes:

> I've read the code and the docs, but I don't think I have a clear idea
> of what "canvases" are and what would be their intended usage.
> Perhaps consider starting the documentation with some introductory
> comments and even a small tutorial.

A canvas is an object that contains memory that can be painted to, and
then displayed by setting it as the `display' text property.

> It sounds like you are talking about a way to create images
> dynamically, but then I don't understand why we need canvas-from-image,
> for example.

It's not really a way to create images dynamically, but rather paint to
a section of the screen.  `canvas-from-image' is just a convenience
function that produces a paintable canvas from a static image.

> Also, is there support for clicking the mouse on a canvas?  I don't
> see it.

You can bind mouse events as usual.

> Well, that'd be my main comments.  I don't quite understand the parts
> of the display code you use for this, they seem like a copy/paste from

Correct, glyph production and iterator code code was mostly copied from
the xwidgets code, since I was somewhat in a rush.  Thanks for the
feedback.

> other objects, sometimes with comments that weren't updated and still
> reference images instead of canvases.

I'll fix that promptly.

> If the drawing on the canvas is  supposed to be modified by Lisp code,
> then we'd need a much more elaborate machinery than just one flag to
> decide when a canvas needs to be redrawn.

Agreed, that is something I've been trying to figure out for some time
now.  Right now, I just do my best to figure out if the canvas has not
touched anything important, and if it has, it just garbages each frame
and calls redisplay.

Attached is some sample code that should hopefully demonstrate exactly
what they are capable of.


[-- Attachment #2: canvas test --]
[-- Type: text/plain, Size: 2879 bytes --]

(setq canvas (make-canvas 1800 1800))

(erase-buffer)
(insert (propertize " " 'display canvas))

(canvas-draw-image canvas (create-image "splash.png")
		   0 0 180 180 (selected-frame) 1.0)
(canvas-ellipse canvas (/ 180 2) (/ 180 2) 130 130 "green")
(canvas-rectangle canvas 0 0 180 180 "red" t)
(canvas-rectangle canvas 20 20 40 100 "white" t 0.5)
(canvas-draw-string canvas 0 0 "Hello, Emacs" "red" 1.0 "monospace" 50)
(canvas-draw-string canvas 60 60 "Hello, Emacs" "white" 1.0 '("monospace" t t) 50)
(let ((image (create-image "splash.png")))
  (canvas-draw-image canvas image 700 20))
(setq track-mouse t)
(defun mouse-movement-canvas-4-paint ()
  "The function to be called when the mouse is moved."
  (interactive "")
  (track-mouse
    (let ((event))
      (while (mouse-movement-p (setq event (read-event)))
	(let* ((position (event-start event))
	       (x (car (posn-x-y position)))
	       (y (cdr (posn-x-y position))))
	  (message (format "%d %d" x y))
	  (canvas-filled-arc canvas x y 100.0 (* 45.0 (/ pi 180.0)) pi "blue"))))))
(defun mouse-movement-canvas-paint ()
  "The function to be called when the mouse is moved."
  (interactive "")
  (track-mouse
    (let ((event))
      (while (mouse-movement-p (setq event (read-event)))
	(let* ((position (event-start event))
	       (x (car (posn-x-y position)))
	       (y (cdr (posn-x-y position))))
	  (message (format "%d %d" x y))
	  (canvas-ellipse canvas x y 10 10 "blue"))))))
(defun mouse-movement-3-canvas-paint ()
  "The function to be called when the mouse is moved."
  (interactive "")
  (track-mouse
    (let ((event))
      (while (mouse-movement-p (setq event (read-event)))
	(let* ((position (event-start event))
	       (x (car (posn-x-y position)))
	       (y (cdr (posn-x-y position))))
	  (message (format "%d %d" x y))
	  (canvas-rectangle canvas x y 80 80 "white"))))))
(defun mouse-movement-2-canvas-paint ()
  "The function to be called when the mouse is moved."
  (interactive "")
  (track-mouse
    (let ((event))
      (while (mouse-movement-p (setq event (read-event)))
	(let* ((position (event-start event))
	       (x (car (posn-x-y position)))
	       (y (cdr (posn-x-y position))))
	  (message (format "%d %d" x y))
	  (canvas-draw-string canvas x y "Welcome to Emacs" nil nil nil 22))))))
(local-set-key [down-mouse-1] #'mouse-movement-canvas-paint)
(local-set-key [down-mouse-3] #'mouse-movement-2-canvas-paint)
(local-set-key [down-mouse-2] #'mouse-movement-3-canvas-paint)
(local-set-key [C-down-mouse-1] #'mouse-movement-canvas-4-paint)
(canvas-measure-string canvas "hi")
(canvas-draw-string canvas 0 0 "Hi" "yellow")
(setq canvas2 (canvas-from-image (create-image "splash.png")))
(canvas-rectangle canvas2 0 0 40 20 "orange")
(canvas-rectangle canvas2 20 0 40 20 "black")

(canvas-rounded-rectangle canvas 20 0 40 20 10.0 "red" nil)
(canvas-draw-canvas canvas canvas2 500 500)

  reply	other threads:[~2020-04-29  9:57 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <875zdikdge.fsf.ref@yahoo.com>
2020-04-29  6:34 ` Emacs canvas support Po Lu via Emacs development discussions.
2020-04-29  8:24   ` Eli Zaretskii
2020-04-29  9:57     ` Po Lu [this message]
2020-04-29 10:10       ` Eli Zaretskii
2020-04-29 10:22         ` Po Lu
2020-04-29 10:27           ` Po Lu via Emacs development discussions.
2020-04-29 11:47             ` Eli Zaretskii
2020-04-29 10:35           ` Eli Zaretskii
2020-04-29 10:41             ` Po Lu
2020-04-29 11:51               ` Eli Zaretskii
2020-04-29 12:12                 ` Po Lu
2020-04-29 16:14               ` David Engster
2020-04-29 16:54                 ` Eli Zaretskii
2020-04-29 17:16                   ` tomas
2020-04-29 17:27                     ` Eli Zaretskii
2020-04-29 17:38                       ` Eli Zaretskii
2020-04-30 13:11                         ` Arthur Miller
2020-04-30 14:18                           ` Eli Zaretskii
2020-04-30 14:58                             ` Arthur Miller
2020-04-30 17:30                               ` Eli Zaretskii
2020-05-01 14:32                                 ` Arthur Miller
2020-04-29 18:51                       ` tomas
2020-04-29 19:03                         ` Eli Zaretskii
2020-04-29 19:08                           ` tomas
2020-04-29 19:25                             ` Eli Zaretskii
2020-04-29 19:59                               ` tomas
2020-04-30  1:19                                 ` Stefan Monnier
2020-04-30  6:55                                   ` tomas
2020-04-30 12:03                                     ` Stefan Monnier
2020-04-30 12:50                                       ` tomas
2020-04-30  8:04                                   ` Po Lu
2020-04-30 12:08                                     ` Stefan Monnier
2020-04-30 13:55                                     ` Eli Zaretskii
2020-05-01 23:27                                       ` Po Lu
2020-05-02  6:51                                         ` Eli Zaretskii
2020-04-30 13:46                                   ` Eli Zaretskii
2020-04-30 14:37                                     ` Stefan Monnier
2020-04-30 17:27                                       ` Eli Zaretskii
2020-04-30 18:22                                         ` Stefan Monnier
2020-04-30 18:42                                           ` Eli Zaretskii
2020-04-30 14:27                                   ` Drew Adams
2020-04-30 13:33                                 ` Eli Zaretskii
2020-04-30 13:52                                   ` Arthur Miller
2020-04-29 19:23                   ` David Engster
2020-04-30 13:29                     ` Eli Zaretskii
2020-04-30  6:52                   ` Corwin Brust
2020-04-29 17:08                 ` Eli Zaretskii
2020-04-29 20:14                   ` David Engster
2020-04-30 13:35                     ` Eli Zaretskii
2020-04-29 20:48   ` Juri Linkov
2020-04-30  2:36     ` Eli Zaretskii
2020-04-30 20:20       ` Juri Linkov
2020-05-01  6:01         ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zhau1uog.fsf@yahoo.com \
    --to=luangruo@yahoo.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).