2017-07-01 77 views
1

我正在尋找一種在簡單的網絡應用程序中繪製圖片的方式。基本上我試圖提交使用標準幻燈片庫的圓形圖片,但它不起作用,我不知道爲什麼。如何使用球拍在網絡應用程序中繪製圖片?

這裏有一個例子:

#lang web-server/insta 

(require slideshow) 

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

你能告訴我一個提示,我怎麼能畫使用標準utils的在我的HTML頁面的圖片?

回答

2

您需要爲圖像生成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