Как встроить js в clojurescript? - PullRequest
0 голосов
/ 12 апреля 2020

Скажем, у меня есть какой-то чистый 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)) без необходимости переписывать всю вещь в cl js. Как я могу это сделать?

1 Ответ

0 голосов
/ 13 апреля 2020

Вы можете взять приведенный выше код и загрузить его на странице перед тегом <script>, который загружает ваш скомпилированный код CL JS. Чтобы загрузить его, вы можете сохранить его в другом файле, например. dimensions.js и загрузите его с тегом <script>, или вы можете встроить его в тот же документ HTML с <script>.

Объект dimensions будет загружен в global контекст, поэтому вы должны иметь доступ к нему как js/dimensions в вашем коде ClojureScript.

...