I need help with my Projects and understanding canvases
Posted: Fri Oct 31, 2014 11:09 am
Hello boys and girls!
I don't really know if this question is appropriate for Support and Development but I will ask it anyways and I apologize for the inconvenience.
I'm currently working on a project that I call "Helium", it's a MS paint clone project and it's a part of a bigger project for me to understand Löve2D better.
Here's the code:
I got some help with the project along the way and I asked another Löve user called Jasoco said that I should use canvases for my project.
Here's what Jasoco said about it:
to understand how canvases works and here's were it gets complicated:
I don't really know how I should implement the canvas function as I don't know how it works, I have checked the wiki and I don't get any wiser.
This is what I want for both projects and for myself:
Myself:
- I want to understand how canvases work
- I want to understand how you can implement saving a image that you have made in Helium
- Is it possible to "draw" within the canvas function?
Helium:
- I want a proper saving function for the project by using the canvas function.
- Again is it possible to draw within the canvas function?
Canvas side project
This project is where I have a square flashing in different colours and I have a UI with a save icon where the idea is that I click on the icon and it screenshots the colour and saves it.
- Here I want to see how canvases works and to gain more knowledge about it.
Once again I'm sorry for the inconvenience and I thank you for taking your time reading this.
Also here's the thread where I ask about canvases:
http://love2d.org/forums/viewtopic.php?f=3&t=78950
I don't really know if this question is appropriate for Support and Development but I will ask it anyways and I apologize for the inconvenience.
I'm currently working on a project that I call "Helium", it's a MS paint clone project and it's a part of a bigger project for me to understand Löve2D better.
Here's the code:
Code: Select all
function love.load()
squares = {}
Lines = {}
currentLine = {0, 0, 0, 0}
lineSize = 16
mouseIsDown = false
-- Colors
colors =
--[[White]]--
{{255, 255, 255},
--[[Blue]]--
{50, 130, 185},
--[[Brown]]--
{143, 114, 30},
--[[Green]]--
{50, 158, 30},
--[[Orange]]--
{199, 129, 68},
--[[Pink]]--
{226, 131, 239},
--[[Purple]]--
{143, 83, 185},
--[[Red]]--
{171, 91, 75},
--[[Turquoise]]--
{50, 137, 151},
--[[Black]]--
{0, 0, 0}}
curColor = 1
-- User Interface
ui = love.graphics.newImage("heliumUI.png")
saveIcon = {x = 675, y = 500}
saveIcon.image = love.graphics.newImage("SaveIcon.png")
saveIcon.namingBarColor = {100, 100, 100}
saveIcon.imageName = "Image_Name"
love.filesystem.setIdentity("helium")
-- White Background
love.graphics.setBackgroundColor(255, 255, 255)
function OpenFolder()
love.system.openURL("file://"..love.filesystem.getSaveDirectory())
end
love.keyboard.setKeyRepeat(true)
end
function love.update( dt )
-- The Brush
local x, y = love.mouse.getPosition()
if love.mouse.isDown( "l" ) and not love.keyboard.isDown("lshift") then
local newPosition = {}
newPosition.x = x - 8
newPosition.y = y - 8
newPosition.color = colors[curColor]
table.insert( squares, newPosition )
end
if mouseIsDown then
currentLine.x2 = x
currentLine.y2 = y
end
end
-- Draws All The Graphics --
function love.draw()
local x, y = love.mouse.getPosition()
love.graphics.setLineWidth(1)
for key, square in ipairs( squares ) do
love.graphics.setColor(square.color)
love.graphics.rectangle( "fill", square.x, square.y, 16, 16 )
end
-- Draws Current Colour
love.graphics.setColor(colors[curColor])
love.graphics.rectangle("fill", 47, 515, 100, 50)
-- Draws A 16x16 Square That Follows The Mouse
if not saveIcon.naming then
love.graphics.rectangle("line", x - 8, y - 8, 16, 16)
end
love.graphics.setLineWidth(lineSize)
for i = 1, #Lines do
love.graphics.setColor(Lines[i].color)
love.graphics.line(Lines[i].x1, Lines[i].y1, Lines[i].x2, Lines[i].y2)
end
love.graphics.setColor(colors[curColor])
love.graphics.line(currentLine.x1, currentLine.y1, currentLine.x2, currentLine.y2)
-- User Interface
love.graphics.setColor(255, 255, 255)
love.graphics.draw(ui, 0, 480)
love.graphics.draw(saveIcon.image, saveIcon.x, saveIcon.y)
if saveIcon.naming then
love.graphics.setColor(saveIcon.namingBarColor)
love.graphics.rectangle("fill", 650, 575, 120, 15)
love.graphics.setColor(0, 0, 0)
love.graphics.print(saveIcon.imageName, 650, 575)
end
end
function love.mousepressed(x, y, button)
-- Drop Tool -- I'll leave this here in case you ever need it.
-- if button == "l" and love.keyboard.isDown("lshift") then
-- local screenshot = love.graphics.newScreenshot()
-- local r, g, b = screenshot: getPixel(x - 1, y - 1)
-- colors[curColor] = {r, g, b}
-- end
if button == "r" then
mouseIsDown = true
currentLine = {x1 = x, y1 = y, x2 = x, y2 = y}
elseif button == "l" then
if x >= saveIcon.x and x <= saveIcon.x + saveIcon.image:getWidth() and
y >= saveIcon.y and y <= saveIcon.y + saveIcon.image:getHeight() then
saveIcon.naming = true
end
end
end
function love.mousereleased(x, y, button)
if button == "r" and mouseIsDown then
mouseIsDown = false
currentLine.color = colors[curColor]
table.insert(Lines, currentLine)
currentLine = {0, 0, 0, 0}
end
end
function love.keypressed(key)
-- Select Your Color By Pressing The Number Keys (1 to 0)
if key == "1" then
curColor = 1 -- White
elseif key == "2" then
curColor = 2 -- Blue
elseif key == "3" then
curColor = 3 -- Brown
elseif key == "4" then
curColor = 4 -- Green
elseif key == "5" then
curColor = 5 -- Orange
elseif key == "6" then
curColor = 6 -- Pink
elseif key == "7" then
curColor = 7 -- Purple
elseif key == "8" then
curColor = 8 -- Red
elseif key == "9" then
curColor = 9 -- Turquoise
elseif key == "0" then
curColor = 10 -- Black
elseif key == "f" then
squares = {}
Lines = {}
love.graphics.setBackgroundColor(colors[curColor])
end
if saveIcon.naming then
if key == "return" then
saveIcon.naming = false
love.mouse.setVisible(false)
local screenShot = love.graphics.newScreenshot()
local image = love.image.newImageData(800, 480)
local tabler = {}
for y = 0, 479 do
for x = 0, 799 do
local r, g, b, a = screenShot:getPixel(x, y)
image:setPixel(x, y, r, g, b, a)
end
end
image:encode(saveIcon.imageName..".png")
love.mouse.setVisible(true)
OpenFolder()
elseif key == "backspace" then
saveIcon.imageName = saveIcon.imageName:sub(1, -2)
end
end
end
function love.textinput(t)
if saveIcon.naming then
saveIcon.imageName = saveIcon.imageName .. t
end
end
Here's what Jasoco said about it:
So I decided to do another side projectA paint program would really benefit from using canvases. Actually they'd pretty much be required. The way you have it now works fine at first, but eventually is going to grind to a halt because you're looping through each square every frame. Plus a canvas could be saved just as easy as a screenshot.
Code: Select all
function love.load()
canvas = love.graphics.newCanvas(800, 480)
-- Images --
-- Floppy Disc Save Icon
saveIcon = love.graphics.newImage("saveIcon.png")
-- Blue User Interface Bar
ui = love.graphics.newImage("blueUI.png")
end
function love.update()
end
function love.draw()
love.graphics.setBackgroundColor(255, 255, 255)
-- Square With Random Color
love.graphics.setColor( math.random(0, 255), math.random(0, 255), math.random(0, 255) )
love.graphics.rectangle("fill", 350, 250, 120, 120)
-- Part Of The User Interface --
-- User Interface
love.graphics.setColor(255, 255, 255)
love.graphics.draw(ui, 0, 480)
-- Save Icon
love.graphics.draw(saveIcon, 675, 500)
end
I don't really know how I should implement the canvas function as I don't know how it works, I have checked the wiki and I don't get any wiser.
This is what I want for both projects and for myself:
Myself:
- I want to understand how canvases work
- I want to understand how you can implement saving a image that you have made in Helium
- Is it possible to "draw" within the canvas function?
Helium:
- I want a proper saving function for the project by using the canvas function.
- Again is it possible to draw within the canvas function?
Canvas side project
This project is where I have a square flashing in different colours and I have a UI with a save icon where the idea is that I click on the icon and it screenshots the colour and saves it.
- Here I want to see how canvases works and to gain more knowledge about it.
Once again I'm sorry for the inconvenience and I thank you for taking your time reading this.
Also here's the thread where I ask about canvases:
http://love2d.org/forums/viewtopic.php?f=3&t=78950