forked from mbk-lab/rui_orig
2
0
Fork 0

Updated Image for wasm

This commit is contained in:
anoshenko 2022-11-02 21:22:15 +03:00
parent c0f60e7bdd
commit 8943be8e91
3 changed files with 62 additions and 3 deletions

View File

@ -1156,6 +1156,37 @@ function loadImage(url) {
img.src = url; img.src = url;
} }
function loadInlineImage(url, content) {
var img = images.get(url);
if (img != undefined) {
return
}
img = new Image();
img.addEventListener("load", function() {
images.set(url, img)
var message = "imageLoaded{session=" + sessionID + ",url=\"" + url + "\"";
if (img.naturalWidth) {
message += ",width=" + img.naturalWidth
}
if (img.naturalHeight) {
message += ",height=" + img.naturalHeight
}
sendMessage(message + "}")
}, false);
img.addEventListener("error", function(event) {
var message = "imageError{session=" + sessionID + ",url=\"" + url + "\"";
if (event && event.message) {
var text = event.message.replace(new RegExp("\"", 'g'), "\\\"")
message += ",message=\"" + text + "\"";
}
sendMessage(message + "}")
}, false);
img.src = content;
}
function clickOutsidePopup(e) { function clickOutsidePopup(e) {
sendMessage("clickOutsidePopup{session=" + sessionID + "}") sendMessage("clickOutsidePopup{session=" + sessionID + "}")
e.stopPropagation(); e.stopPropagation();

View File

@ -1,6 +1,12 @@
package rui package rui
import "strconv" import (
"encoding/base64"
"path/filepath"
"runtime"
"strconv"
"strings"
)
const ( const (
// ImageLoading is the image loading status: in the process of loading // ImageLoading is the image loading status: in the process of loading
@ -76,6 +82,27 @@ func (manager *imageManager) loadImage(url string, onLoaded func(Image), session
image.listener = onLoaded image.listener = onLoaded
image.loadingStatus = ImageLoading image.loadingStatus = ImageLoading
manager.images[url] = image manager.images[url] = image
if runtime.GOOS == "js" {
if file, ok := resources.images[url]; ok && file.fs != nil {
dataType := map[string]string{
".svg": "data:image/svg+xml",
".png": "data:image/png",
".jpg": "data:image/jpg",
".jpeg": "data:image/jpg",
".gif": "data:image/gif",
}
ext := strings.ToLower(filepath.Ext(url))
if prefix, ok := dataType[ext]; ok {
if data, err := file.fs.ReadFile(file.path); err == nil {
session.callFunc("loadInlineImage", url, prefix+";base64,"+base64.StdEncoding.EncodeToString(data))
return image
} else {
DebugLog(err.Error())
}
}
}
}
session.callFunc("loadImage", url) session.callFunc("loadImage", url)
return image return image
} }

View File

@ -119,8 +119,9 @@ func (bridge *wasmBridge) callCanvasVarFunc(v any, funcName string, args ...any)
func (bridge *wasmBridge) callCanvasImageFunc(url string, property string, funcName string, args ...any) { func (bridge *wasmBridge) callCanvasImageFunc(url string, property string, funcName string, args ...any) {
image := js.Global().Get("images").Call("get", url) image := js.Global().Get("images").Call("get", url)
if !image.IsUndefined() && !image.IsNull() { if !image.IsUndefined() && !image.IsNull() && !bridge.canvas.IsNull() {
result := image.Call(funcName, args...)
result := bridge.canvas.Call(funcName, append([]any{image}, args...)...)
if property != "" { if property != "" {
bridge.canvas.Set(property, result) bridge.canvas.Set(property, result)
} }