It is absolutely correct to have the commas where you placed them. I explain below.
In part of my spare time, I work on fun projects.
"In part of my spare time" is an adverbial phrase and in formal contexts, I would definitely retain the comma, as it precedes the subject of the sentence. A comma should always be used in this way, but it is sometimes acceptable to omit it when the adverbial is short enough that leaving out the comma will not hamper the reader's grasp of the intended meaning. Thus, a sentence such as
Occasionally I work on fun projects
is acceptable in informal contexts. However, one is more likely to find a comma inserted in formal situations:
Occasionally, I work on fun projects
Now, to the second sentence:
When I have spare time, I work on fun projects.
"When I have spare time" is a dependent clause, or more specifically, an adverbial clause. As it precedes the independent clause, it should be followed by a comma. Again, it is possible in some contexts to leave out the comma when the preceding independent or adverbial clause is short enough that leaving it out would not obscure the intended meaning, but contexts such as these are hard to find. In publications like novels or magazines, one might readily find a construction such as this:
When he comes we are going to have a ball
This reads easily without a comma. Consider this, though:
When the sky is darkened on many a stormy night, I curl up beside my lamp
and read my son to sleep
It would be difficult to argue for the omission of a comma in this case.
The comma also indicates a pause in communication. The writer can thus choose to insert it or leave it out to create the desired effect. You could bear this in mind while deciding on your commas. Include them where you feel a pause makes sense.