It was after the work contract ended but visa was still valid
Even if the date was valid, an H1B is linked to a valid I-797 and if you don't have a valid I-797 from the current employer or a new employer, the visa will be cancelled, as you found out. You can't enter on an H1B without a corresponding valid I-797.
On the face of it, it looks like you were trying to reenter the US illegally. You can't really expect the officer to believe that you were coming back for a holiday and thought that an H1B would allow you entry for personal reasons. That's not plausible even though it may be true! H1B workers are well aware that their visa is linked to their job - if you somehow didn't know this, it may be hard to convince the officer.
You will need a very strong explanation otherwise you can expect trouble with your Canadian visa application.