racket / pict

Other
15 stars 20 forks source link

Add functions to save pict as a file #35

Closed leafac closed 5 years ago

leafac commented 6 years ago

Similar to save-image and save-svg-image in 2htdp/image.

Sample code by @zenspider:

#lang racket
(require racket/draw metapict metapict/graph)

(set-curve-pict-size 500 500)  ; width and height of image
(ahlength  1.0)                ; size of arrow head 
(define f sin)
(define p
  (with-window (window -12 12 -12 12)             ; xmin, xmax, ymin, ymax
    (draw (draw-arrow (curve (pt -10   0) -- (pt 10  0)))  ; x-axis
          (draw-arrow (curve (pt   0 -10) -- (pt  0 10)))  ; y-axis
          (curve (pt -3 1) -- (pt -2 2))
          (label-rt  "x" (pt 10.2 0))                      ; label for x axis
          (label-top "y" (pt 0 10.2))                      ; label for y axis
          (color "blue" (draw (circle (pt 2 1) 3)))        ; center (2,1) radius 3
          (color "red"  (draw (graph f -10 10 #:samples 100))))))
p ; show pict in repl
;(define (save-pict-as-svg p width height filename [exists 'replace])
;  (define dc (new svg-dc%
;                  [width width]
;                  [height height]
;                  [output filename]
;                  [exists exists]))
;  (send dc start-doc "An SVG Test")  ; a message
;  (send dc start-page)
;  (draw-pict p dc 0 0)
;  (send dc end-page)
;  (send dc end-doc))
;
;  
;(save-pict-as-svg p 300 300 "output.svg")
leafac commented 6 years ago

@blerner added: See http://docs.racket-lang.org/file/convertible.html?q=file%2Fconvertible — essentially, (convert your-pict 'svg-bytes) should give you a bytestring containing a valid SVG.