如何使用球拍在网络应用程序中绘制图片?

问题描述:

我正在寻找一种在简单的网络应用程序中绘制图片的方式。基本上我试图提交使用标准幻灯片库的圆形图片,但它不起作用,我不知道为什么。如何使用球拍在网络应用程序中绘制图片?

这里有一个例子:

#lang web-server/insta 

(require slideshow) 

(define (start request) 
    (response/xexpr '(html 
        (body (circle 10)) 
        ) 
) 
) 

你能告诉我一个提示,我怎么能画使用标准utils的在我的HTML页面的图片?

您需要为图像生成HTML IMG标记并提供正确的内容。一种方法是直接把图像源在网页中作为data uri来源:

#lang web-server/insta 
(require pict 
     net/base64 
     file/convertible) 

;; pict->data-uri : Pict -> String 
(define (pict->data-uri pict) 
    (format "data:image/png;base64,~a" 
      (base64-encode (convert pict 'png-bytes)))) 

;; start : Request -> Response 
(define (start request) 
    (send/suspend/dispatch 
    (lambda (make-url) 
    (response/xexpr 
     `(html 
     (body 
     (p "hello, here's a circle:") 
     (img ([src ,(pict->data-uri (circle 10))])))))))) 

另一种方法是使图像源的URL,将图像发送作为其响应:

#lang web-server/insta 
(require pict 
     file/convertible) 

;; pict-response : Pict -> Response 
(define (pict-response pict) 
    (response/full 200 #"Ok" 
       (current-seconds) #"image/png" 
       (list) 
       (list (convert pict 'png-bytes)))) 

;; start : Request -> Response 
(define (start request) 
    (send/suspend/dispatch 
    (lambda (make-url) 
    (response/xexpr 
     `(html 
     (body 
     (p "hello, here's a circle:") 
     (img ([src ,(make-url 
         (lambda (req) 
         (pict-response (circle 10))))])))))))) 
+0

感谢您的回答,但是我可以使用多参数程序绘制图片吗?这似乎是一个问题(圆20“solid”“blue”) – Dimitry

+0

@Dimitry,来自'pict'库的'circle'函数与来自'2htdp/image'的函数不同。使用'pict'库的纯蓝色圆圈的代码是'(colorize(disk 20)'blue')'。或者你可以用'2hdp/image'替换'pict'的require行,因为这个库的图像也可以转换成PNG字节。你甚至可以一起使用这两个库,因为'pict'库函数通常可以接受'2htdp/image'图像作为参数(但据我所知,其他方式不会)。你需要使用'prefix-in'来处理名称冲突。 –

+0

谢谢你的解释。我已经做了一个修复,它运行良好 – Dimitry