canvasAPI

optimized API for creating displays in your mods


this library allows you to create displays in your mods!
the library is completely free and distributed under the MIT license


NES Emulator


this mod allows you to emulate your favorite games from the NES platform right inside Scrap Mechanic!
this is a real emulator running inside the game, you can even connect a second controller and play with your friends!
just connect the console to the display and the joysticks to the console. insert the cartridge and play the NES right inside Scrap Mechanic!
unfortunately, due to the restricted API of the game, when pressing several buttons in the joystick GUI, bugs sometimes occur and other buttons are released. if you install betterAPI, the button processing works fine, otherwise it is better to connect the controller with the seat.
you can connect the joystick to the seat to control the game from it. to import other control buttons, use the "Control Import" unit and connect it to the joystick.
to upload your games, there is a "flash cartridge" that requires betterAPI. press 0 on the joystick to open the folder where you can to add games (host only).
to emulate sound, there is a "speaker" block. betterAPI is needed for its operation.

type: a mod based on canvasAPI


installation

  1. unzip the files from the archive to the "/Scripts/canvasAPI" directory in your mod
  2. copy the files from the "/Scripts/canvasAPI/mod_files" directory to the root of your mod
  3. if you are using a newer version of writing blocks using shapedb, you will need to register the paths to json from canvasAPI there
  4. add the do dofile("$CONTENT_DATA/Scripts/canvasAPI/canvas.lua") to the first line of the scripts where you want to use the display

canvasAPI


                dofile("$CONTENT_DATA/Scripts/canvasAPI/canvas.lua")
test1 = class()

local function color(...)
    return canvasAPI.formatColorToSmallNumber(sm.color.new(...))
end

function test1:client_onCreate()
    self.canvas = canvasAPI.createCanvas(self.interactable, 64, 64)
    self.canvas.setRenderDistance(64)
    self.rotation = 0
end

function test1:client_onFixedUpdate()
    self.canvas.update()
    
    --------------------------------------- motion

    self.canvas.setOffset(sm.vec3.new(0, 2.5 + (math.sin(math.rad(sm.game.getCurrentTick())) / 8), 0))
    self.canvas.setCanvasRotation(sm.vec3.new(0, self.rotation, 0))
    self.rotation = self.rotation + 0.25
    if not self.canvas.isRendering() then return end

    --------------------------------------- random fill

    local stack = {}
    for i = 1, 64 do
        canvasAPI.pushData(stack, canvasAPI.draw.fill, math.random(0, self.canvas.sizeX - 1), math.random(0, self.canvas.sizeY - 1), math.random(0, 16), math.random(0, 16), color(math.random() / 3, math.random() / 3, 0))
    end

    canvasAPI.pushData(stack, canvasAPI.draw.rect, 0, 0, self.canvas.sizeX, self.canvas.sizeY, color(1, 1, 1), 1)
    canvasAPI.pushData(stack, canvasAPI.draw.set, 0, 0, color(0, 1, 0))
    canvasAPI.pushData(stack, canvasAPI.draw.set, self.canvas.sizeX - 1, 0, color(1, 0, 0))
    canvasAPI.pushData(stack, canvasAPI.draw.set, self.canvas.sizeX - 1, self.canvas.sizeY - 1, color(1, 1, 0))
    canvasAPI.pushData(stack, canvasAPI.draw.set, 0, self.canvas.sizeY - 1, color(0, 0, 1))
    canvasAPI.pushData(stack, canvasAPI.draw.set, 0, 1, color(0, 1, 1))
    canvasAPI.pushData(stack, canvasAPI.draw.set, 1, 0, color(0, 1, 1))

    --------------------------------------- pushing

    self.canvas.pushStack(stack)
    self.canvas.flush()
end

function test1:client_onDestroy()
    self.canvas.destroy()
end
            
                dofile("$CONTENT_DATA/Scripts/canvasAPI/canvas.lua")
test2 = class()
test2.displaySize = 1
test2.displayPosition = sm.vec3.new(0, 0.25, 0.25)
test2.displayRotation = sm.vec3.new(-32, 0, 0)

function test2:client_onCreate()
    self.display = canvasAPI.createClientScriptableCanvas(self.interactable, 64, 64, -test2.displaySize / 64, test2.displayPosition, test2.displayRotation)
    self.display.setOptimizationLevel(16)
    --self.display.setFont(canvasAPI.fonts.lgc_5x5)

    self.width = self.display.getWidth()
    self.height = self.display.getHeight()
    self.perlinSize = (1 / self.width) * 5
end

function test2:client_onFixedUpdate()
    self.display.update()

    self.pos = (self.pos or 0) + 1
    if self.display.getAudience() > 0 then
        --self.display.clear()
        local pos = math.floor(self.pos)
        for ix = 0, self.width - 1 do
            for iy = 0, self.height - 1 do
                self.display.drawPixel(ix, iy, sm.color.new(sm.noise.perlinNoise2d((ix + pos) * self.perlinSize, iy * self.perlinSize, 0), 0, 0))
            end
        end
        self.display.drawText(1, 1, "HELLO, WORLD!")
        self.display.flush()
    end
end

function test2:client_onDestroy()
    self.display.destroy()
end