如何在clojurescript中嵌入js?

问题描述 投票:0回答:1

说我有一些纯粹的js代码,例如:

    dimensions = {
        max_height : 800,
        max_width  : 600,
        width  : 800, // this will change
        height : 600, // this will change
        largest_property : function () {
            return this.height > this.width ? "height" : "width";
        },
        read_dimensions : function (img) {
            this.width = img.width;
            this.height = img.height;
            return this;
        },
        scaling_factor : function (original, computed) {
            return computed / original;
        },
        scale_to_fit : function () {
            var x_factor = this.scaling_factor(this.width,  this.max_width),
                y_factor = this.scaling_factor(this.height, this.max_height),

                largest_factor = Math.min(x_factor, y_factor);

            this.width  *= largest_factor;
            this.height *= largest_factor;
        }
    };

dimensions.read_dimensions(img).scale_to_fit();

canvas.width  = dimensions.width;
canvas.height = dimensions.height;
context.drawImage(img, 0, 0, dimensions.width, dimensions.height);

并且我想在clojurescript中将这个代码与(.drawImage context img 0 0 (.-width dimensions) (.-height dimensions))一起使用,而不必在cljs中重写整个代码。我该怎么办?

javascript clojurescript
1个回答
0
投票
您可以将上面的代码加载到<script>标签之前的页面中,该标签将加载您已编译的CLJS代码。要加载它,可以将其保存在其他文件中,例如。 dimensions.js并使用<script>标记加载,或者也可以将其嵌入与<script>相同的HTML文档中。

dimensions对象将被加载到global上下文中,因此您应该能够在ClojureScript代码中以js/dimensions的身份对其进行访问。

© www.soinside.com 2019 - 2024. All rights reserved.