Sun and Moon demo

Active Server Paint

Home Page

Coherent Visual

 

 

Sun and Moon demo

This demonstration of Active Server Paint creates a image that shows the sun or moon in the sky based on the current time in San Diego.

>> Execute the demonstration <<

Using AS Paint makes creating solutions like this fairly simple. Examine the source code below to see how this image was created using Active Server Page code in the form of VB Script. 

If you own Active Server Paint, click here to download (6K) the Sun and Moon commented source code and bitmap for your own testing, modification, and use or see the code below.


Sun and Moon source code:

<HTML>
<HEAD>
<TITLE>ASPaint Sun and Moon Demo</TITLE>
</HEAD>
<BODY BGCOLOR=#FFFF09>
<CENTER><H2>Active Server Paint Sun & Moon demo</H2></CENTER>
<P>
<P>This demonstration creates a sun or moon and places it in the sky at the arc appropriate for the current time. <B>Active Server Paint</B> uses the base image of a simple blue button style image with the ASPaint words. The sky, heavenly body (sun or moon), the horizon, and the current date and time string are drawn onto the image.
<P>As the sun moves toward noon, the sky lightens. Toward sunset the sky darkens until the moon rise at 6 pm.
<P>
<%
' Create variable to hold hour
Dim curHour
' Create variables for the center of the horizon
Dim centerX, centerY
' Create a variable for pi since VB Script doesn't
' have constants
Dim pi
pi = 3.14159

Public Sub asptDrawMoon(tempObject, myHour, myCenterX, myCenterY)
If myHour < 7 Then
    ' if hour is before 7 am use this equation
trigval = ((12 - myHour) / 12) * pi
Else
trigval = ((-(myHour - 12)) / 12) * pi
End If
    ' Draw the sky in a rectangle
tempObject.asptDrawRect myCenterX - 125, myCenterY - 50, myCenterX + 125, myCenterY + 60, RGB(0, 0, 0), RGB(0, 0, 0), 0
    ' Determine the x & y of the heavenly body,
    ' in this case the moon
heavenBodyX = myCenterX + 70 * Sin(trigval)
heavenBodyY = (myCenterY + 40) + 70 * Cos(trigval)
    ' Draw original white circle against black night sky
tempObject.asptDrawEllipse heavenBodyX - 10, heavenBodyY - 10, heavenBodyX + 10, heavenBodyY + 10, RGB(255, 255, 255), RGB(255, 255, 255), 0
    ' Draw overlap to make it a crescent moon
tempObject.asptDrawEllipse heavenBodyX - 15, heavenBodyY - 15, heavenBodyX + 5, heavenBodyY + 5, RGB(0, 0, 0), RGB(0, 0, 0), 0
    ' Draw the horizon in case the moon is just
    ' rising or setting
tempObject.asptDrawRect myCenterX - 125, myCenterY + 40, myCenterX + 125, myCenterY + 60, RGB(0, 0, 255), RGB(0, 0, 255), 0
End Sub

Sub asptDrawSun(tempObject, myHour, myCenterX, myCenterY)
    ' Determine the value based on the hour
trigval = ((24 - myHour) / 12) * pi
    ' Set the x & y values of the sun
heavenBodyX = myCenterX + (70 * Sin(trigval))
heavenBodyY = (myCenterY + 40) + 70 * Cos(trigval)
    ' Determine the sky tones are values of
    ' blue (skytoneB) and green (skytoneG)
skytoneB = Abs(CInt(100 * Cos(trigval))) + 100
skyToneG = Abs(CInt(100 * Cos(trigval)))

    ' Draw sky in predetermined tone
tempObject.asptDrawRect myCenterX - 125, myCenterY - 50, myCenterX + 125, myCenterY + 60, RGB(0, skyToneG, skytoneB), RGB(0, skyToneG, skytoneB), 0

    ' Draw little circle rays around the sun
For i = 1 To 6
trigValue = i * pi / 3
tX = heavenBodyX + 14 * Sin(trigValue)
tY = heavenBodyY + 14 * Cos(trigValue)
tempObject.asptDrawEllipse tX - 2, tY - 2, tX + 2, tY + 2, RGB(255, 255, 0), RGB(255, 255, 0), 0
Next
    ' Draw main sub orb
tempObject.asptDrawEllipse heavenBodyX - 10, heavenBodyY - 10, heavenBodyX + 10, heavenBodyY + 10, RGB(255, 255, 0), RGB(255, 255, 0), 0
    ' Draw horizon in case sun in rising or setting
tempObject.asptDrawRect myCenterX - 125, myCenterY + 40, myCenterX + 125, myCenterY + 60, RGB(0, 0, 255), RGB(0, 0, 255), 0
End Sub

    ' Create Active Server Paint object
Set myobj = Server.CreateObject("ASPaint.ctlASPaint")

    ' Set output file name
    outPicname = CStr("c:\sunmoon.gif")
    ' Set i to the hour with fraction (i.e. 6:30 = 6.5)
i = Hour(Now) + (Minute(Now) / 60)
    ' Load main background image
myobj.asptLoadImage "c:\webshare\asptweb\sunmoon.bmp"

    ' Is it daylight (6am-6pm) or night
If i >= 6 And i <= 18 Then
asptDrawSun myobj, i, 150, 70
Else
asptDrawMoon myobj, i, 150, 70
End If

    ' Set transparent background text mode
myobj.asptSetTextMode 1
    ' Set the text color to white
myobj.asptSetTextColor RGB(255, 255, 255)
    ' Draw the current date & time in Comic Sans font
tempstr = "The current date and time: " + CStr(Now)
myobj.asptDrawText CStr(tempstr), "Comic Sans MS", 9, 0, 0, 0, 40, 155, RGB(255, 255, 255)
    ' Save the completed image
myobj.asptSaveImage CStr(outPicName)
    ' Clear the image buffer
myobj.asptClearBuffer
%>
<P><B>Active Server Paint</B> has completed generating the image.
Here it is:<P>

<IMG SRC = "<%=outPicname%>">

<P>Click <A HREF="default.htm">here</A> to return to ASPaint Demo.<P><P>

<P>
<P>
<P>
</BODY>
</HTML>


The Active Server Paint control can be purchased from VBXtras. Click here to jump directly to the ASPaint product..

 

Coherent Visual, 4141 Jutland Dr., Suite 303, San Diego, CA 92117
619-581-0344

LinkExchange
LinkExchange Member